Finding account nature of a contact group?

后端 未结 2 1079
一个人的身影
一个人的身影 2021-01-06 05:14

I am developing an application in which it is required to find the nature of a contact group means whether it is google group , phone group or sim group. How to find it.Plea

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 05:58

    The code below prints the contact name and type. I have not optimized it and it will print multiple records but I think you will know what to do.

    package com.example.android.contactmanager;
    import android.app.Activity;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.RawContacts;
    import android.util.Log;
    
    public final class ContactManager extends Activity{
    
    /**
     * Called when the activity is first created. Responsible for initializing the UI.
     */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        printContactList();
    }
    
    /**
     * Print contact data in logcat.
     * SIM : Account_Type = com.anddroid.contacts.sim
     * Phone : Depends on the manufacturer e.g For HTC : Account_Type = com.htc.android.pcsc
     * Google : Account_Type = com.google
     */
    private void printContactList() {
        Cursor cursor = getContacts();
        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {
            Log.d("Display_Name", cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
            Log.d("Account_Type", cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE)));
            cursor.moveToNext();
    
        }
    }
    
    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Data.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                RawContacts.ACCOUNT_TYPE
        };
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    
        return managedQuery(uri, projection, null, selectionArgs, sortOrder);
    }
    }
    

提交回复
热议问题