2017-03-06 16 views
-1

私は頻繁に呼ばれるリストを取得しようとしています。 CONTENT_STREQUENT_URIはあなたにスターを付けて頻繁に欲しいとあなたに与えます。androidのよく連絡先を読む方法

カーソルc = this.getContentResolver()。クエリ(Contacts.CONTENT_STREQUENT_URI、 null、null、null、null);

+0

「ContactsContract.Contacts.STARRED」アイテムを「フィルタリング」しようとしましたか? – pskink

+0

あなたは私にいくつかのアイデアをどのようにresult.Actually私は頻繁に連絡を与えるが、私のアプリは頻繁な連絡先を与えるネイティブのアプリを頻繁にコールをクリアした場合は、lenevoデバイスで頻繁に、私は何をすべきか混乱しています –

+0

3番目の 'query'メソッドの第4パラメータを参照 – pskink

答えて

0

これは、あなたが検索している頻繁な/スター付きの連絡先を取得するのに役立ちます。カーソルから情報を引き出す方法の例として、getPhoneNumbersメソッドを書きました。また、あなたのデバイスから連絡先を削除/スタートすると、自動的にあなたのUIは更新されません。データを再同期する必要があります。これを行うには、いくつかの方法があります。私の意見では、このデータをもう一度再取得すれば、あまりにも多くのスター付きの連絡先を持っていないので、それほど悪くはありません。しかし、入力サイズが大きければ、毎回このデータを取得するよりも、このリストのページを区切り、より洗練された方法を見つけることが望ましいでしょう。

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

これは私に頻繁にダイヤル番号 –

関連する問題