2017-03-06 70 views
-1

我想要經常調用列表。 CONTENT_STREQUENT_URI可讓您頻繁出演,而且我只是經常想要。如何閱讀android中的頻繁聯繫人

Cursor c = this.getContentResolver()。查詢(Contacts.CONTENT_STREQUENT_URI, null,null,null,null);

+0

試圖「過濾掉」ContactsContract.Contacts.STARRED項目? – pskink

+0

你能給我一些想法如何過濾結果。實際上,我得到了頻繁和加星標的聯繫人,但在lenevo設備上,如果我清除頻繁的調用本機應用程序不顯示任何頻繁的調用,但我的應用程序仍然給予頻繁的聯繫。我很困惑我該怎麼辦 –

+0

查看第三個查詢方法的第四個參數 – pskink

回答

0

這可能會幫助您獲取您正在搜索的頻繁/加星標的聯繫人。我爲您寫了getPhoneNumbers方法,作爲如何從光標中提取信息的示例。另外,請注意,如果您從設備/ unstar中刪除聯繫人,這不會自動更新您的用戶界面。您將需要重新同步您的數據。要做到這一點,有幾種方法。在我看來,如果你只是重新獲取這些數據,它不會太糟糕,因爲你可能沒有太多的加星標的聯繫人。但是,對於更大的輸入大小,您將希望對此列表進行分頁並找到比每次獲取此數據更優雅的方式。

public static List<? extends ContactsModel> getFreqContacts(Context context) { 
     List<ContactsModel> contacts = new ArrayList<>(); 
     ContentResolver contentResolver = context.getContentResolver(); 
     Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, "starred=?", new String[]{"1"}, "upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC"); 

     if(cursor.getCount() > 0) { 
      while (cursor.moveToNext()) { 
       final String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       final String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       final Set<String> phone_numbers = getContactPhoneNumber(contentResolver, cursor, contact_id); 
       final Set<String> emails = getContactEmailAddress(contentResolver, contact_id); 

       // do whatever you want. Now you have the phone numbers and emails of the contacts you want 
      } 
     } 
     cursor.close(); 

     Collections.sort(faveContactsModels); 
     return faveContactsModels; 
    } 

    private static Set<String> getContactPhoneNumber(ContentResolver contentResolver, Cursor cursor,String id) { 
     Set<String> contactNumbers = new HashSet<>(); 
     if(Integer.parseInt(cursor.getString(
      cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0 
      ) { 
      Cursor pCur = contentResolver.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "= ?", 
       new String[]{id}, null 
      ); 
      while(pCur.moveToNext()) { 
       String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       contactNumbers.add(phoneNo); 
      } 
      pCur.close(); 
     } 
     return contactNumbers; 
    } 
+0

這將只給我加星標的聯繫人我需要經常撥打的號碼 –