How to identify local phone contact and SYNC phone contact in Android?

Can anyone help me find a solution for the following problem.

  • I have to determine if a phone contact is saved locally or from email? (programmatically)

I read from google doc that ContactsContract.Groups which contains information about raw contact groups like Gmail contact groups. The current API does not support the concept of groups spanning multiple accounts.

Based on this, I tried the following code.

 StringBuffer output = new StringBuffer();
   final String[] GROUP_PROJECTION = new String[] {
            ContactsContract.Groups._ID, 
            ContactsContract.Groups.TITLE,
            ContactsContract.Groups.SUMMARY_WITH_PHONES
            };

Cursor c =  getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, GROUP_PROJECTION, null,
        null, ContactsContract.Groups.TITLE);

  int IDX_ID = c.getColumnIndex(ContactsContract.Groups._ID);
  int IDX_TITLE = c.getColumnIndex(ContactsContract.Groups.TITLE);
  output.append("title"+IDX_TITLE+"\n");
    Map<String,GroupInfo> m = new HashMap<String, GroupInfo>();

while (c.moveToNext()) {
    output.append("test...\n");
    GroupInfo g = new GroupInfo();
    g.id = c.getString(IDX_ID);
    g.title = c.getString(IDX_TITLE);
    output.append("title"+c.getString(IDX_TITLE)+"\n");
    int users = c.getInt(c.getColumnIndex(ContactsContract.Groups.SUMMARY_WITH_PHONES));
    if (users>0) {
        // group with duplicate name?
        GroupInfo g2 = m.get(g.title);
        if (g2==null) {
            m.put(g.title, g);
            output.append("title"+g.title+"\n");
            groups.add(g);
        } else {
            g2.id+=","+g.id;
        }
    }
}
outputText.setText(output);
c.close();

      

but there is no hope.

+3


source to share


2 answers


I am posting this answer for future use. We can distinguish between local phone contacts and sync contacts using the field calledRawContacts.SOURCE_ID

Described here

SOURCE_ID
read / write
A string that uniquely identifies this string in the original account. Typically, it is set while the original contact is inserted and is not subsequently changed. One notable exception is the new raw contact: it will have an account name and type (and possibly a dataset), but not the original ID. This indicates to the sync adapter that a new contact should be created on the server side and its ID stored in the corresponding SOURCE_ID field on the phone.



Below is a sample code, it gives id for sync contacts and null for others.

private void testContact() {
        StringBuffer output = new StringBuffer();
        ContentResolver resolver = getContentResolver();
        Cursor contacts = resolver.query(Contacts.CONTENT_URI, null,
                Contacts.HAS_PHONE_NUMBER + " != 0", null, Contacts._ID
                        + " ASC");
        Cursor data = resolver.query(Data.CONTENT_URI, null, Data.MIMETYPE
                + "=? OR " + Data.MIMETYPE + "=?", new String[]{
                Email.CONTENT_ITEM_TYPE, Phone.CONTENT_ITEM_TYPE},
                Data.CONTACT_ID + " ASC");

        int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
        int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
        int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
        int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
        boolean hasData = data.moveToNext();

        while (contacts.moveToNext()) {
            long id = contacts.getLong(idIndex);
            Uri rawContactUri = 
                      ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);

                    Uri entityUri =
                      Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

                    Cursor c =
                      getContentResolver().query(
                        entityUri,
                        new String[] {
                                  RawContacts.ACCOUNT_NAME,
                          RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
                        null, null, null);

                    try {
                         while (c.moveToNext()) {
                             String sourceId = c.getString(0);
                             if (!c.isNull(1)) {

                                 String source_id = c.getString(1);
                             try {
                                     output.append(c.getString(4)+sourceId+" "+source_id+"\n");

                                     //output.append(datas+ "Sync1 "+  c.getString(4)+" Sync2 "+  c.getString(5)+" Sync3"+  c.getString(6)+" Sync4 "+  c.getString(7)+"\n");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                 //decide here based on mimeType, see comment later
                             }
                         }
                    } finally {
                         c.close();
                    }

        }

        outputText.setText(output);
    }

      

+2


source


Read this and see if it can ACCOUNT_TYPE

help you



+1


source







All Articles