简体   繁体   中英

Nexus 4 dialer - Creating contact without phone number

I am trying to create a contact programatically without a phone number. I manage to create it but when I open the dialer it crashes. If I open the contacts it is OK.

Also, if I add a contact manually without a phone number it works OK.

I am working with a Nexus 4 - KitKat 4.4.2.

This is the code I use:

/**
   * 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) {
          }
   }

I get the following when I open the dialer:

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

I have tried not putting the number at all, putting null and putting an empty string and still the same.

We tried using a different dialler and all works ok.

Is it a problem with the dialler or am I missing some field I should be setting?

The right way is to not add a number. Adding null or even an empty string would lead to the same crash.

Also, since you are doing a new insert it will not update an existing contact. You will need to do newUpdate for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM