如何以编程方式将新的Android联系人插Phone中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (33)

我正在编写一个应用程序,让用户从他们的Android设备中删除联系人并将其存储在单独的数据库中。我将这些信息恢复到手机的功能如下所示:

public void addContact(Contact contact)
{
    ContentValues values = new ContentValues();
    values.put("contact_id", contact.getContactId());
    values.put("lookup", contact.getLookupKey());
    values.put("mimetype", StructuredName.CONTENT_ITEM_TYPE);
    values.put("data1", contact.getStructuredName().getDisplayName());
    values.put("data2", contact.getStructuredName().getGivenName());
    values.put("data3", contact.getStructuredName().getFamilyName());
    values.put("data4", contact.getStructuredName().getPrefix());
    values.put("data5", contact.getStructuredName().getMiddleName());
    values.put("data6", contact.getStructuredName().getSuffix());
    context.getContentResolver().insert(Data.CONTENT_URI, values);
}

我也试着把这个代码放在结构化名称的插入之上,但是无济于事:

values.put("_id", contact.getContactId());
values.put("lookup", contact.getLookupKey());
values.put("display_name", contact.getStructuredName().getDisplayName());
context.getContentResolver().insert(Contacts.CONTENT_URI, values);
values.clear();

这是我的应用程序崩溃时的logcat错误日志:

05-07 20:31:53.941: E/Trace(819): error opening trace file: No such file or directory (2)
05-07 20:34:39.035: D/AndroidRuntime(853): Shutting down VM
05-07 20:34:39.048: W/dalvikvm(853): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-07 20:34:39.197: E/AndroidRuntime(853): FATAL EXCEPTION: main
05-07 20:34:39.197: E/AndroidRuntime(853): java.lang.NullPointerException
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.os.Parcel.readException(Parcel.java:1431)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.content.ContentResolver.insert(ContentResolver.java:866)
05-07 20:34:39.197: E/AndroidRuntime(853):  at com.protextyou.contacts.ContactHandler.addContact(ContactHandler.java:58)
05-07 20:34:39.197: E/AndroidRuntime(853):  at com.protextyou.StartPage$12.onClick(StartPage.java:542)
05-07 20:34:39.197: E/AndroidRuntime(853):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:137)
05-07 20:34:39.197: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 20:34:39.197: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
05-07 20:34:39.197: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:511)
05-07 20:34:39.197: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 20:34:39.197: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 20:34:39.197: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
提问于
用户回答回答于

你可以试下这段代码:

ContentValues values = new ContentValues();
            values.put(Data.RAW_CONTACT_ID, 001);
            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            values.put(Phone.NUMBER, "1-800-GOOG-411");
            values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
            values.put(Phone.LABEL, "Nirav");
            Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
用户回答回答于
你可以试下这段代码:
ArrayList<ContentProviderOperation> ops =
      new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
      .withValue(Data.RAW_CONTACT_ID, rawContactId)
      .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
      .withValue(Phone.NUMBER, "1-800-GOOG-411")
      .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
      .withValue(Phone.LABEL, "free directory assistance")
      .build()); 
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

扫码关注云+社区