2017-04-25 64 views

回答

3

這是我們如何能做到這一點

通姓名和號碼的數組此方法。

public static void addToContactList(Context context, String strDisplayName, String[] strNumber) throws Exception { 

    ArrayList<ContentProviderOperation> cntProOper = new ArrayList<>(); 
    int contactIndex = cntProOper.size();//ContactSize 
    ContentResolver contactHelper = context.getContentResolver(); 

    cntProOper.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)//Step1 
      .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
      .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build()); 

    //Display name will be inserted in ContactsContract.Data table 
    cntProOper.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)//Step2 
      .withValueBackReference(android.provider.ContactsContract.Data.RAW_CONTACT_ID, contactIndex) 
      .withValue(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
      .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, strDisplayName) // Name of the contact 
      .build()); 

    for (String s : strNumber) { 
     //Mobile number will be inserted in ContactsContract.Data table 
     cntProOper.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)//Step 3 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, contactIndex) 
       .withValue(android.provider.ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, s) // Number to be added 
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); //Type like HOME, MOBILE etc 
    } 

    ContentProviderResult[] s = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, cntProOper); //apply above data insertion into contacts list 

    for (ContentProviderResult r : s) { 
     Log.i(TAG, "addToContactList: " + r.uri); 
    } 
} 
+0

這應該是正確的答案。你能告訴我如何添加照片 – myatmins

相關問題