ArrayList<ContentProviderOperation> ops =new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 3)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "999999999")
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.DISPLAY_NAME, "hhhhhhh")
.withValue(ContactsContract.CommonDataKinds.Email.DATA, "abcd@gmail.com")
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, 4)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, "999999999")
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.DISPLAY_NAME, "hhhlllllllllll")
.withValue(ContactsContract.CommonDataKinds.Email.DATA, "efgh@gmail.com")
.build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我已经添加了
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
权限。我遇到了以下错误:
09-25 09:30:41.365: W/System.err(1057): android.content.OperationApplicationException: insert failed
09-25 09:30:41.375: W/System.err(1057): at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:161)
09-25 09:30:41.375: W/System.err(1057): at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:461)
09-25 09:30:41.375: W/System.err(1057): at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:225)
09-25 09:30:41.375: W/System.err(1057): at android.content.ContentResolver.applyBatch(ContentResolver.java:901)
09-25 09:30:41.375: W/System.err(1057): at com.example.projdemo.MainActivity.onCreate(MainActivity.java:136)
09-25 09:30:41.375: W/System.err(1057): at android.app.Activity.performCreate(Activity.java:5008)
09-25 09:30:41.385: W/System.err(1057): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-25 09:30:41.385: W/System.err(1057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-25 09:30:41.385: W/System.err(1057): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-25 09:30:41.385: W/System.err(1057): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-25 09:30:41.385: W/System.err(1057): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-25 09:30:41.395: W/System.err(1057): at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 09:30:41.395: W/System.err(1057): at android.os.Looper.loop(Looper.java:137)
09-25 09:30:41.395: W/System.err(1057): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 09:30:41.395: W/System.err(1057): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:30:41.395: W/System.err(1057): at java.lang.reflect.Method.invoke(Method.java:511)
09-25 09:30:41.395: W/System.err(1057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 09:30:41.395: W/System.err(1057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 09:30:41.405: W/System.err(1057): at dalvik.system.NativeStart.main(Native Method)
AGGREGATION_MODE
行被注释掉。这解决了我在tab2中遇到的问题。在我的情况下,联系人没有被添加到tab2中。除此之外,这个应用在其他设备上运行良好。 - user2756345