0

i am create an application which use contact to display. My problem is that i can show all conacts (from my email accounts,skype and other), but i want show only contacts from my device (with images like i am getting now), like when open native contacts app and go to Contacts->Menu->Settings->Contacts->Contact to display->Device contacts. I am not very well understand Contact content provider and need a help. There a lot of posts on stackoverflow, bu t i cant find post for my requirements. For retreive all contact i am using this CursorLoader

CursorLoader cL = new CursorLoader(getActivity(),

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

This is my criteria and projections:

 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
};
Alexandro
  • 170
  • 3
  • 12

1 Answers1

0

After some time , i am return to search answer to thisquestion. the answer maybe is not correct byt it helps me.

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;

}

After my query posted in question i am filter contacts by contactId and check their ACCOUNT_TYPE , in my logs i see on samsung vnd.sec.contact.phone(means that is phone contact) and on my lg with 2 sim i found this SIM Account. Perhaps there is better solution, but i figure it out this way.

Alexandro
  • 170
  • 3
  • 12