2017-03-17 5 views
0

Ich bin Symbol meiner App im Telefonbuch hinzufügen. Das Problem ist jetzt, dass seine adaequat in Api Ebene < 23 aber nicht auf Api Ebene arbeiten> 23.Hinzufügen App-Symbol auf Kontakt funktioniert nicht in Marshmallow

in API 23 wird die neuen Kontakt mit der Nummer erzeugt.

in Api 21 Api 21

in Api 23 enter image description here

String MIMETYPE = "vnd.android.cursor.item/com.appiconincontact"; 

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 
       // insert account name and account type 


       ops.add(
         ContentProviderOperation 
           .newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true)) 
           .withValue(RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME) 
           .withValue(RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE) 
           .withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT) 
           .build() 
       ); 


       // insert contact number 
       ops.add(ContentProviderOperation 
         .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) 
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
         .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 
         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number) 
         .build()); 

       // insert mime-type data 
       ops.add(ContentProviderOperation 
         .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true)) 
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 
         .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE) 
         .withValue(ContactsContract.Data.DATA2, Constants.APP_NAME) 
         .withValue(ContactsContract.Data.DATA3, "User Connected with " + number) 
         .build()); 

       try { 
        resolver.applyBatch(ContactsContract.AUTHORITY, ops); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

Antwort

2

Sie ein neueRawContact zu schaffen, und in der Hoffnung, dass das System in eine aggregiert bestehende Contact.

Sie vermissen den Teil "Bitte fügen Sie diesen neuen Rohkontakt in diesen vorhandenen Kontakt ein".

Dazu müssen Sie eine AggregationExceptions hinzufügen.

Zuerst die aktuellen RawContact IDs im Contact Sie möchten finden Sie eine Linie zu AggregationExceptions, die zwischen Ihrem neuen RawContact._ID (raw1) und einer bestehenden RawContact._ID (raw2)

Builder builder = ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI); 
builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); 
builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, raw1); 
builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, raw2); 
ops.add(builder.build()); 

EDIT Links hinzuzufügen, fügen Sie dann

Wenn Sie diesen Code in Ihre bestehende Batch hinzufügen:

ArrayList<ContentProviderOperation> ops = new ArrayList<>(); 
// insert account name and account type 
ops.add(ContentProviderOperation.newInsert(...).build()); 
// insert contact number 
ops.add(ContentProviderOperation.newInsert(...).build()); 
// insert mime-type data 
ops.add(ContentProviderOperation.newInsert(...).build()); 

// add an AggregationExceptions line 
ops.add(ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI) 
    .withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER) 
    .withValueBackReference(AggregationExceptions.RAW_CONTACT_ID1, 0) 
    .withValue(AggregationExceptions.RAW_CONTACT_ID2, theRawContactIdOfTheExistingContact) 
    .build()); 

try { 
    resolver.applyBatch(ContactsContract.AUTHORITY, ops); 
} catch (Exception e) { ... } 

Das einzige, was Sie brauchen hier ausfüllen theRawContactIdOfTheExistingContact ist zu beachten, dass es nicht eine Kontaktnummer, es ist eine Roh-Kontakt-ID, müssen Sie dort den richtigen Wert setzen, je nach den Rest Ihres Codes und wie Sie den Kontakt finden, dem Sie Ihre Daten hinzufügen möchten.

+0

können Sie den vollständigen Code dafür hinzufügen. es funktioniert nicht –

+0

hinzugefügt Code, der zeigt, wie diese Operation passt in Ihre bestehende Batch – marmor

+0

im Erstellen von ** row-contact_id ** über diesen Link http://StackOverflow.com/Questions/19675279/getting-rawContact-Id-using- Kontakt-ID, aber nicht funktioniert –

Verwandte Themen