想要添加家庭、办公室等。使用以下代码将号码添加到单个联系人姓名
ArrayList<Phone> phones = contatsetget.getPhones();
if(phones != null){
for(Phone phone:phones){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,phone.getType())
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,phone.getNo())
.build());
System.out.println("Phone type...."+phone.getType());
System.out.println("Phone number...."+phone.getNo());
System.out.println("Phone rowId...."+phone.getRowId());
}
}
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}catch(Exception e){
e.printStackTrace();
}
在默认应用程序中打开此联系人手机时,会出现如下错误
错误日志
06-24 12:13:55.676: E/ContentValues(11527): Cannot parse Integer value for Mobile at key data2
06-24 12:13:55.676: D/AndroidRuntime(11527): Shutting down VM
06-24 12:13:55.676: W/dalvikvm(11527): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-24 12:13:55.698: E/AndroidRuntime(11527): FATAL EXCEPTION: main
06-24 12:13:55.698: E/AndroidRuntime(11527): java.lang.NullPointerException
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.model.dataitem.DataItem.getKindTypeColumn(DataItem.java:187)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailFragment$DetailViewEntry.fromValues(ContactDetailFragment.java:1261)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailFragment.buildEntries(ContactDetailFragment.java:564)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailFragment.bindData(ContactDetailFragment.java:453)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailFragment.setData(ContactDetailFragment.java:370)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailLayoutController.showContactWithoutUpdates(ContactDetailLayoutController.java:474)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.detail.ContactDetailLayoutController.setContactData(ContactDetailLayoutController.java:309)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.contacts.activities.ContactDetailActivity$3$1.run(ContactDetailActivity.java:230)
06-24 12:13:55.698: E/AndroidRuntime(11527): at android.os.Handler.handleCallback(Handler.java:725)
06-24 12:13:55.698: E/AndroidRuntime(11527): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 12:13:55.698: E/AndroidRuntime(11527): at android.os.Looper.loop(Looper.java:137)
06-24 12:13:55.698: E/AndroidRuntime(11527): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-24 12:13:55.698: E/AndroidRuntime(11527): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 12:13:55.698: E/AndroidRuntime(11527): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 12:13:55.698: E/AndroidRuntime(11527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 12:13:55.698: E/AndroidRuntime(11527): at dalvik.system.NativeStart.main(Native Method)
06-24 12:13:55.726: W/ActivityManager(277): Force finishing activity com.android.contacts/.activities.ContactDetailActivity
06-24 12:13:55.737: W/WindowManager(277): Failure taking screenshot for (246x410) to layer 21015
06-24 12:13:56.036: I/Choreographer(277): Skipped 50 frames! The application may be doing too much work on its main thread.
06-24 12:13:56.046: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
06-24 12:13:56.276: W/ActivityManager(277): Activity pause timeout for ActivityRecord{40f29838 u0 com.android.contacts/.activities.ContactDetailActivity}
06-24 12:14:05.761: W/ActivityManager(277): Launch timeout has expired, giving up wake lock!
06-24 12:14:06.374: W/ActivityManager(277): Activity idle timeout for ActivityRecord{40f9d428 u0 com.android.contacts/.activities.PeopleActivity}