2016-09-30 38 views
2

我正在與一個應用程序一起使用,我正在與手機通訊錄進行交互。 我想添加一個新的聯繫人到我的手機聯繫人列表。我試過下面的代碼,但它不起作用:以編程方式通過我的應用程序插入新聯繫人而不使用意圖

void addContact(Context ctx, PreviewContactModel model) { 

     ArrayList<Contact_Model> contact_models = handler.getAllContacts(); 

     Contact_Model contact_model = new Contact_Model(); 

     ArrayList<ContentProviderOperation> contentProviderOperation = new ArrayList<>(); 

     int rawContactID = Integer.parseInt(contact_models.get(contact_models.size() - 1).getContactId()) + 1; 

     contact_model.setContactId(String.valueOf(rawContactID)); 

     contentProviderOperation.add(ContentProviderOperation 
       .newInsert(ContactsContract.RawContacts.CONTENT_URI) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
       .build()); 

     contact_model.setContactAccount(new ContactAccount(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE)); 

     // ------------------------------------------------------ Names 
     if (model.getName() != null) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(
          ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 
          model.getName()).build()); 
      contact_model.setContactName(model.getName()); 
     } 

     // First Name 

     if (!model.getFirstName().equals("")) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(
          ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, 
          model.getFirstName()).build()); 
      contact_model.setFirstName(model.getFirstName()); 
     } 


     // Last Name 
     if (!model.getLastName().equals("")) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(
          ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, 
          model.getLastName()).build()); 
      contact_model.setLastName(model.getLastName()); 
     } 

     // Contact Photo 

     if (model.getContactPhoto() != null) { 
      contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, model.getContactPhoto()) 
        .build()); 

      try { 
       contact_model.setContactPhoto(new String(model.getContactPhoto(), "UTF-8")); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 

     } 

     // Contact Note 

     if (!model.getNote().equals("")){ 
      contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Note.NOTE, model.getNote()) 
        .build()); 
      contact_model.setNote(model.getNote()); 
     } 

     // Contact Birthday Date 

     if (!model.getDob().equals("")){ 
      contentProviderOperation.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1) 
        .withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Event.DATA, model.getDob()) 
        .build()); 
      contact_model.setDob(model.getDob()); 
     } 


     // ------------------------------------------------------ Mobile Number 
     if (model.getMap().get(0) != null) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
          model.getMap().get(0)) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
        .build()); 
      contact_model.setContactNumber(model.getMap().get(0)); 
     } 

     // ------------------------------------------------------ Home Numbers 
     if (model.getMap().size() > 1) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
          model.getMap().get(1)) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_HOME) 
        .build()); 
     } 

     // ------------------------------------------------------ Work Numbers 
     if (model.getMap().size() > 2) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, 
          model.getMap().get(2)) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_WORK) 
        .build()); 
     } 

     // ------------------------------------------------------ workEmail 
     if (model.getEmail().size() > 0) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, 
          model.getEmail().get(0)) 
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
          ContactsContract.CommonDataKinds.Email.TYPE_WORK) 
        .build()); 
      contact_model.setContactEmail(model.getEmail().get(0)); 
     } 
     // ------------------------------------------------------ homeEmail 
     if (model.getEmail().size() > 1) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, 
          model.getEmail().get(1)) 
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
          ContactsContract.CommonDataKinds.Email.TYPE_HOME) 
        .build()); 
     } 
     // ------------------------------------------------------ Organization 
     if (!model.getOrganization().equals("") && !model.getJob().equals("")) { 
      contentProviderOperation.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactID) 
        .withValue(
          ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) 
        .withValue(
          ContactsContract.CommonDataKinds.Organization.COMPANY, 
          model.getOrganization()) 
        .withValue(
          ContactsContract.CommonDataKinds.Organization.TYPE, 
          ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 
        .withValue(
          ContactsContract.CommonDataKinds.Organization.TITLE, 
          model.getJob()) 
        .withValue(
          ContactsContract.CommonDataKinds.Organization.TYPE, 
          ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 
        .build()); 
      contact_model.setOrganization(model.getOrganization()); 
      contact_model.setJob(model.getJob()); 
     } 
     // Asking the Contact provider to create a new contact 
     try { 
      ctx.getContentResolver() 
        .applyBatch(ContactsContract.AUTHORITY, contentProviderOperation); 
      handler.addContact(contact_model); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      //show exception in toast 
      Toast.makeText(ctx, "Exception: " + e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

如果我做錯了什麼,請糾正我。提前致謝。

+1

那麼什麼樣的'Exception'弄來? 「」它不工作「是什麼意思? – pskink

+0

我沒有得到任何例外@pskink。當函數調用沒有例外發生,當我檢查我的手機聯繫人列表聯繫人沒有被添加。 –

回答

1

試試這個代碼

 String displayName = "ABC"; 
     String emailID  = "[email protected]"; 
     String mobileNumber = "88888888"; 
     String homeNumber = "1111"; 
     String workNumber = "2222"; 
     String company  = "xyz"; 
     String jobTitle  = "boss"; 

     ArrayList<ContentProviderOperation> contentProviderOperationArrayList = new ArrayList <ContentProviderOperation>(); 

     contentProviderOperationArrayList.add(ContentProviderOperation.newInsert(
       ContactsContract.RawContacts.CONTENT_URI) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
       .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
       .build()); 

     //Name 
     if (displayName != null) { 
      contentProviderOperationArrayList.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, 
          displayName).build()); 
     } 

     //Mobile Number 
     if (mobileNumber != null) { 
      contentProviderOperationArrayList.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, mobileNumber) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
        .build()); 
     } 

     //Home 
     if (homeNumber != null) { 
      contentProviderOperationArrayList.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, homeNumber) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_HOME) 
        .build()); 
     } 

     //Work 
     if (workNumber != null) { 
      contentProviderOperationArrayList.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, workNumber) 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
          ContactsContract.CommonDataKinds.Phone.TYPE_WORK) 
        .build()); 
     } 

     //Email 
     if (emailID != null) { 
      contentProviderOperationArrayList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
        .withValue(ContactsContract.Data.MIMETYPE, 
          ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID) 
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) 
        .build()); 
     } 

     //Organization 
     if (!company.equals("") && !jobTitle.equals("")) { 
      contentProviderOperationArrayList.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, company) 
        .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 
        .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle) 
        .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 
        .build()); 
     } 

     // Creating new contact 
     try { 
      getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderOperationArrayList); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(yourContext, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
+0

我已經使用這個代碼@Febi Mathew,但它不工作,同樣的問題。聯繫人沒有被添加到聯繫人列表中,沒有例外。我也檢查了這個[鏈接](http://stackoverflow.com/a/4744514/6583944) –

+0

檢查您是否已在清單中添加了必需的權限。如果您使用運行棉花糖及以上版本的設備,請檢查您的應用程序是否被授予添加聯繫人。 –

+0

是的,我已經在清單'<使用權限android:name =「android.permission.READ_CONTACTS」/> '中添加了讀寫權限,並且在運行時授予權限@Fabi Mathew。 –

相關問題