2016-02-18 66 views
0

我創建了一個使用聯繫人進行顯示的應用程序。我的問題是,我可以顯示所有conacts(從我的電子郵件帳戶,Skype和其他),但我想只顯示我的設備上的聯繫人(像我現在得到的圖像),如當打開本機聯繫人應用程序,並去往通訊錄 - >菜單 - >設置 - >聯繫人 - >聯繫人顯示 - >設備聯繫人。我不太瞭解聯繫內容提供商,需要幫助。有很多帖子在stackoverflow,我不能找到我的要求的職位。 對於retreive所有接觸我用這CursorLoader使用CursorLoader查找設備聯繫人

CursorLoader cL = new CursorLoader(getActivity(), 

       contentUri, 
       ContactsQuery.PROJECTION, 
       ContactsQuery.SELECTION, 
       null, 
       ContactsQuery.SORT_ORDER 
     ); 

這是我的標準和預測:

final static int QUERY_ID = 1; 

// A content URI for the Contacts table 
final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 

// The search/filter query Uri 
final static Uri FILTER_URI = ContactsContract.Contacts.CONTENT_FILTER_URI; 

// The selection clause for the CursorLoader query. The search criteria defined here 
// restrict results to contacts that have a display name and are linked to visible groups. 
// Notice that the search on the string provided by the user is implemented by appending 
// the search string to CONTENT_FILTER_URI. 
@SuppressLint("InlinedApi") 
final static String SELECTION = 
     (Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME) + 
       "<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "= 1 AND "+ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"; 


// The desired sort order for the returned Cursor. In Android 3.0 and later, the primary 
// sort key allows for localization. In earlier versions. use the display name as the sort 
// key. 
@SuppressLint("InlinedApi") 
final static String SORT_ORDER = 
     Utils.hasHoneycomb() ? ContactsContract.Contacts.SORT_KEY_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME; 

// The projection for the CursorLoader query. This is a list of columns that the Contacts 
// Provider should return in the Cursor. 
@SuppressLint("InlinedApi") 
final static String[] PROJECTION = { 

     // The contact's row id 
     ContactsContract.Contacts._ID, 

     // A pointer to the contact that is guaranteed to be more permanent than _ID. Given 
     // a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate 
     // a "permanent" contact URI. 
     ContactsContract.Contacts.LOOKUP_KEY, 

     // In platform version 3.0 and later, the Contacts table contains 
     // DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or 
     // some other useful identifier such as an email address. This column isn't 
     // available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME 
     // instead. 
     Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME, 

     // In Android 3.0 and later, the thumbnail image is pointed to by 
     // PHOTO_THUMBNAIL_URI. In earlier versions, there is no direct pointer; instead, 
     // you generate the pointer from the contact's ID value and constants defined in 
     // android.provider.ContactsContract.Contacts. 
     Utils.hasHoneycomb() ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID, 

     // The sort order column for the returned Cursor, used by the AlphabetIndexer 
     SORT_ORDER, 
     ContactsContract.Contacts.HAS_PHONE_NUMBER 
}; 

回答

0

一段時間後,我返回搜索答案thisquestion。 答案可能是不正確的,它可以幫助我。

public static boolean getConnections(Context context, String contactId) { 
    // Read all data for contactId 
    String selection = ContactsContract.RawContacts.CONTACT_ID + " = ?"; 
    String[] selectionArgs = new String[]{contactId}; 

    Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null); 
    String accountType = null; 
    while (c.moveToNext()) { 
     accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE)); 
    } 
    Log.e("getConnections", accountType + ""); 
    c.close(); 
    if (accountType.equals("SIM Account") || accountType.equals("vnd.sec.contact.phone")) 
     return true; 
    else 
     return false; 

} 

我的查詢問題發佈後我被過濾的ContactID接觸,並檢查他們ACCOUNT_TYPE,在我的日誌我看到三星vnd.sec.contact.phone(是指電話聯繫)和我LG與2 sim我找到了這個SIM卡帳戶。 也許有更好的解決方案,但我想通過這種方式。

相關問題