2017-03-17 68 views
0

我在電話簿中添加我的應用程序的圖標。現在的問題是,它在Api級別工作良好,但在Api級別> 23上工作不正常。在聯繫人上添加應用程序圖標不工作在棉花糖

在API 23中,它創建了帶號碼的新聯繫人。

在阿比21 Api 21

在阿比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(); 
       } 

回答

2

你正在創建一個新的RawContact,並且希望該系統彙總成一個現有Contact

您錯過了「請將此新的原始聯繫人添加到此現有聯繫人」部分。

爲此,您需要添加AggregationExceptions

首先,找到您要添加到,然後添加一行AggregationExceptions您的新RawContact._IDraw1)和現有的RawContact._IDraw2

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

編輯之間的聯繫Contact當前RawContact IDs

如果要將此代碼添加到您的現有批次:

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) { ... } 

你需要在這裏填寫的唯一事情是theRawContactIdOfTheExistingContact,請注意,這是不是接觸-ID,它是一個原料接觸-ID,你需要把正確的價值在那裏,這取決於剩下的代碼以及如何找到聯繫人來添加數據。

+0

你可以爲它添加完整的代碼嗎?它不起作用 –

+0

添加代碼,顯示此操作如何適合您的現有批次 – marmor

+0

即時通訊創建** raw-contact_id **使用此鏈接http://stackoverflow.com/questions/19675279/getting-rawcontact-id-using- contact-id但不工作 –

相關問題