我正在尝试在没有电话号码的情况下以编程方式创建联系人。我设法创建它,但是当我打开拨号器时它崩溃了。如果我打开联系人就可以了。
另外,如果我在没有电话号码的情况下手动添加联系人,它可以正常工作。
我正在使用 Nexus 4 - KitKat 4.4.2。
这是我使用的代码:
/**
* Add new contact to contacts list and contacts DB.
* @param contact
* @param ContactKey
* @param ctx
*/
public void addContact(Retrievable contact,String name,String ContactKey ,final Activity ctx){
initializeDB(ctx);
if(Groupid== null)
Groupid = getGroupId(ctx);
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());
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, name)
.build());
//need to check because if is null it crash dialer in SDK 4.4.2
if(contact.phone != null && contact.phone !=" "){
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, contact.phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
else{
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, new String())
.build());
}
if(contact.phone2 != null && contact.phone2 !=" "){
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, contact.phone2)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
if(contact.phone3 != null && contact.phone3 !=" "){
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, contact.phone3)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
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, contact.fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_FAX_WORK)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,(" ".equals(contact.getAddress())?contact.location:contact.getAddress()))
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,ctx.getResources().getString(R.string.meuhedet))
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, Long.parseLong(Groupid))
.build());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap photo = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.logo_meuhedet);
photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
.build());
try {
ContentProviderResult [] res =ctx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
rawContactID = ContentUris.parseId(res[0].uri);
boolean result = db.saveContactsIntoDatabase(rawContactID, contact.index, ContactKey);
if(result){
((Activity) ctx).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, R.string.successfully_saved_contact, Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
}
}
当我打开拨号器时,我得到以下信息:
FATAL EXCEPTION: AsyncTask #4
Process: com.google.android.dialer, PID: 8233
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.IllegalArgumentException: the bind value at index 2 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at com.android.dialer.database.DialerDatabaseHelper.insertUpdatedContactsAndNumberPrefix(DialerDatabaseHelper.java:632)
at com.android.dialer.database.DialerDatabaseHelper.updateSmartDialDatabase(DialerDatabaseHelper.java:784)
at com.android.dialer.database.DialerDatabaseHelper$SmartDialUpdateAsyncTask.doInBackground(DialerDatabaseHelper.java:481)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
我试过根本不放数字,放空并放一个空字符串,但还是一样。
我们尝试使用不同的拨号器,一切正常。
这是拨号器的问题还是我错过了我应该设置的一些字段?