2016-06-08 11 views

Antwort

0

seine Kopie Vergangenheit vielleicht wird es Ihnen helfen,

 String DisplayName = "XYZ"; 
    String MobileNumber = "123456"; 
    String HomeNumber = "1111"; 
     String WorkNumber = "2222"; 
     String emailID = "[email protected]"; 
    String company = "bad"; 
    String jobTitle = "abcd"; 


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()); 

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

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

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

//------------------------------------------------------ Email 
    if (emailID != null) {ops.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("")) { 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,  
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()); 
} 

// Asking the Contact provider to create a new contact     
    try { 
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
e.printStackTrace(); 
Toast.makeText(myContext, "Exception: " + e.getMessage(),  
Toast.LENGTH_SHORT).show(); 
} 
+0

Ich fragte nach Adresse Feld hier Adresse nur nicht da @ Shivpal Jodha – android

1

ich gleiche Problem haben. Nach einer langen Zeit habe ich eine Lösung, so können Sie diesen Code unten verwenden:

public boolean addContact() { 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (haveContactPermission()) { 
      ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
      int rawContactInsertIndex = ops.size(); 

      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, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, TextViewUserFullName.getText()) // Name of the person 
        .build()); 


      ops.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, homephone) // Number of the person 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build()); // Type of mobile number 

      ops.add(ContentProviderOperation 
        .newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(
          ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilephone) // Number of the person 
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); // Type of mobile number 

      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, getBitmapFromURL(image)) 
        .build()); 

      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email) 
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME) 
        .build()); 


      ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) 
        .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, TextViewWifeName.getText()).build()); 

      try { 
       ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
       return true; 
      } catch (RemoteException e) { 
       Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show(); 
       // error 
       return false; 
      } catch (OperationApplicationException e) { 
       Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show(); 
       // error 
       return false; 
      } 
     } else { 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, 
        PERMISSION_CONTACT_NUMBER); 
      return false; 
     } 

    } else { 


     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
     int rawContactInsertIndex = ops.size(); 

     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, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, TextViewUserFullName.getText()) // Name of the person 
       .build()); 



     ops.add(ContentProviderOperation 
       .newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(
         ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, homephone) // Number of the person 
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME).build()); // Type of mobile number 

     ops.add(ContentProviderOperation 
       .newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(
         ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobilephone) // Number of the person 
       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build()); // Type of mobile number 

     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, getBitmapFromURL(image)) 
       .build()); 

     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email) 
       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME) 
       .build()); 


     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 
       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex) 
       .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) 
       .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, TextViewWifeName.getText()).build()); 

     try { 
      ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
      return true; 
     } catch (RemoteException e) { 
      Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show(); 
      // error 
      return false; 
     } catch (OperationApplicationException e) { 
      Toast.makeText(ContactInfoZone.this, "" + e.toString(), Toast.LENGTH_SHORT).show(); 
      // error 
      return false; 
     } 
    } 
} 

Ich denke, das wird Ihnen helfen.

+0

Was ist der Unterschied in beiden wenn und sonst Code? – android

+0

ok thks .... können Sie mir einige Tipps zum schnellen Lesen Kontakt geben .... lesen 400 Kontakte dauert es 2 Minuten .... bitte sagen Sie mir eine schnelle Möglichkeit, Kontakte mit allen Daten abzufragen. – android

+0

Es gibt zwei Anzahl von-wenn-Bedingungen. Erste für Version ist über 23 oder nicht? Wenn die Version unter 23 dann kein Problem ist, können wir direkt Kontakt hinzufügen. sonst wenn Bedingung ist über 23 dann (zweite wenn Bedingung ist hier) wir wollen um Kontakt Erlaubnis geben ist oder nicht, wenn ja, dann können wir Kontakt hinzufügen, wenn nein dann erstellen Berechtigungen. –