在无法在棉花糖中使用的联系人上添加应用程序图标 [英] Add app icon on Contact not working in Marshmallow
问题描述
我正在电话簿中添加我的应用程序图标.现在的问题是,它在Api级别< 23,但不适用于Api级别> 23.
在API 23中,它使用数字创建新的联系人.
在Api 21中
在Api 23中
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();
}
您正在创建新 RawContact
,并希望系统将其聚合到现有的Contact
中.>
您缺少请将此新的原始联系人附加到此现有联系人中"部分.
为此,您需要添加 AggregationExceptions
首先,在要添加的Contact
中找到当前的RawContact IDs
,然后在AggregationExceptions
中添加一行,以链接新的RawContact._ID
(raw1
)和现有的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());
编辑
如果要将此代码添加到现有批次中:
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,而是 raw-contact-id ,您将需要在其中放置正确的值,具体取决于代码的其余部分以及如何找到要向其中添加数据的联系人.
I am adding icon of my app in phonebook. now the problem is that its working fine in Api level < 23 but not working on Api level > 23.
in API 23 it creates new Contact with number.
in Api 21
in Api 23
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();
}
You're creating a new RawContact
, and hoping that the system aggregates it into an existing Contact
.
You're missing the "please attach this new raw-contact into this existing contact" part.
To do that you need to add an AggregationExceptions.
First, find the current RawContact IDs
in the Contact
you wish to add to, then add a line to AggregationExceptions
that links between your new RawContact._ID
(raw1
) and an existing 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
If you want to add this code to your existing batch:
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) { ... }
The only thing you need to fill in here is theRawContactIdOfTheExistingContact
, note that it's not a contact-id, it's a raw-contact-id, you'll need to put the right value there, depending on the rest of your code, and how you find the contact to add your data to.
这篇关于在无法在棉花糖中使用的联系人上添加应用程序图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!