2016-09-14 4 views
2

Wie kann ich neue Kontakt in meiner Kontaktliste programmgesteuert mit mehr als einer Nummer erstellen? Ich versuche, den nächsten Code:Erstellen von Kontakt mit zwei oder mehr Telefonnummern in Android

public static void createNewContact(Context ctx, String phone, String name, String note) { 
    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, 
        name) // 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, phone) // 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.Note.CONTENT_ITEM_TYPE) 
      .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note) // note of the person 
         .build()); 

    try { 
     ContentProviderResult[] res =ctx.getContentResolver(). 
       applyBatch(ContactsContract.AUTHORITY, ops); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

Und in Programm i schreiben:

createNewContact(context, "+12345", "nameContact"); 
    createNewContact(context, "+67890", "nameContact"); 

Danach gibt es zwei Kontakte mit dem Namen "nameContact". Aber ich muss einen Kontakt "nameContact" mit zwei Nummern erstellen: "+12345" und "+67890".

+0

Warum eine Reihe von Telefonen nicht einfügen? Sie können sich nicht auf den Kontaktnamen verlassen. Wenn Sie später Telefone hinzufügen möchten, benötigen Sie die Kontakt-ID oder die URL. Wenn Sie beispielsweise mehrere Konten auf Ihrem Telefon haben, die mit einem Kontakt interagiert haben, haben Sie so viele Kontakte mit demselben Namen wie Sie Konten haben. –

+0

wenn ich Array-Nummern einfüge [] = {"+12345", "+67890"}; und code sieht aus wie withValue (ContactsContract.CommonDataKinds.Phone.NUMBER, numbers), ich bekomme die Ausnahme IllegalArgumentException: bad value type: [Ljava.lang.String; –

Antwort

1

Ich habe nicht die Zeit, zu testen, aber ich wurde darüber nachgedacht:

public static void createNewContact(Context ctx, ArrayList<String> phones, String name, String note) { 

.... 

for (String phone : phones) { 
    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, phone) // Number of the person 
      .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
      .build()); // Type of mobile number 
} 

....

+0

danke, es funktioniert! –

Verwandte Themen