2015-01-10 57 views
0

我需要添加聯繫人的列表,我使用這個代碼:添加聯繫人 - 沒有錯誤,但接觸沒有得到補充

private void addContact(String name, String phone, Integer id) { 
    ContentValues values = new ContentValues(); 
    values.put(ContactsContract.Data.RAW_CONTACT_ID, id); 
    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
    values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone); 
    values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM); 
    values.put(ContactsContract.CommonDataKinds.Phone.LABEL, name); 
    Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    for (int i = 0; i < 10; i++) { 
     addContact("name_" + i, "+112233445566" + i, i); 
    } 
    } 

#Manifest: 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="android.me123.com.me456" > 

    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 


#Gradle: 

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 20 
    buildToolsVersion "20" 

    defaultConfig { 
     applicationId "android.me123.com.me456" 
     minSdkVersion 15 
     targetSdkVersion 20 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
} 

而就什麼都不會發生,沒有任何錯誤。這是怎麼回事?

如何設置新聯繫人的姓名? Phone.LABEL不是我正在尋找的。

回答

0

代替Phone.LABEL使用ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME下面是完整的方法來添加聯繫人

public void addtocontact(String DisplayName, String MobileNumber) { 
     // TODO Auto-generated method stub 

     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 

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

     // ------------------------------------------------------ 
     // Names 
     if (DisplayName != null) { 
      ops.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, 0) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(
          ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 
          DisplayName).build()); 
     } 

     // ------------------------------------------------------ 
     // Mobile Number 
     if (MobileNumber != null) { 
      ops.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, 0) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
          MobileNumber) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
        .build()); 
     } 
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 

    } 
相關問題