Android有意图编辑联系人列表

时间:2015-02-04 10:35:32

标签: android android-intent contact

我试图按意图编辑联系人。我的代码是:

Uri mUri = ContentUris.withAppendedId(Phone.CONTENT_URI,
                            Long.parseLong(mContact.str) );
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setDataAndType(mUri, Contacts.CONTENT_ITEM_TYPE);
intent.putExtra("finishActivityOnSaveCompleted", true);
mActivity.startActivityForResult(intent, EDIT_CONTACT_RESULT);

其中mContact.str是联系人Contacts._ID。该代码适用于具有不同Android版本的不同设备。但是我在没有任何崩溃的情况下在华为4.2.2上得到以下错误:

02-04 14:01:23.024: E/ContactLoader(8190): Error loading the contact: content://com.android.contacts/data/phones/22127
02-04 14:01:23.024: E/ContactLoader(8190): java.lang.IllegalArgumentException: uri format is unknown
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.util.ContactLoaderUtils.ensureIsContactUri(ContactLoaderUtils.java:64)
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.model.ContactLoader.loadInBackground(ContactLoader.java:425)
02-04 14:01:23.024: E/ContactLoader(8190):  at com.android.contacts.model.ContactLoader.loadInBackground(ContactLoader.java:82)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:303)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:68)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:56)
02-04 14:01:23.024: E/ContactLoader(8190):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-04 14:01:23.024: E/ContactLoader(8190):  at java.lang.Thread.run(Thread.java:838)

此外,我在我的清单文件中使用READ_CONTACTSWRITE_CONTACTS权限。

2 个答案:

答案 0 :(得分:0)

  Uri mUri = ContentUris.withAppendedId(Phone.CONTENT_URI,
                            Long.parseLong(mContact.str) );
  Intent intent = new Intent(Intent.ACTION_EDIT);
  intent.setDataAndType(mUri, Contacts.CONTENT_ITEM_TYPE);
  intent.putExtra("finishActivityOnSaveCompleted", true);
  //add the below line 
  intent.addFlag(intent.FLAG_ACTIVITY_CLEAR_TOP);
  mActivity.startActivityForResult(intent, EDIT_CONTACT_RESULT);

答案 1 :(得分:0)

好的,我终于找到了解决方案! URI应该是这样的:

Uri mUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                                Long.parseLong(mContact.str));

mContact.str也应如下所示:

mContact.str = phones.getString(phones
                            .getColumnIndex(Phone._ID));