从Android API中的Native Contact App启动我的应用程序

时间:2017-08-08 06:37:30

标签: android android-contacts android-syncadapter

我正在尝试将手机中的所有联系人与我的应用程序相关联。然后从联系人应用程序启动我的应用程序在api< 23它工作正常。但是在api> 23中,联系人会被复制。所以让我知道有人试过这个!!这是我试过的......!

 public class ContactsSync extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String name, number, id;
        for (int i = 0; i <=mIDs.size(); i++) {
            //name = mNames.get(i);
            id = mIDs.get(i);
            number = mNumbers.get(i);
            ContactsManager.addContact(Paymoney.this, new MyContact(id, number));
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        getContactDataAfter();
    }
}

public static void addContact(Context context,MyContact contact){

    ContentResolver resolver = context.getContentResolver();
    boolean mHasAccount = isAlreadyRegistered(resolver, contact.Id);

    if(mHasAccount){
    } else {

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        // insert account name and account type
        ops.add(ContentProviderOperation
                .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constants.ACCOUNT_NAME)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constants.ACCOUNT_TYPE)
                .withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
                        ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
                .build());

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


        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.DATA1, 12345)
                .withValue(ContactsContract.Data.DATA2, "user")
                .withValue(ContactsContract.Data.DATA3, "Send Money")
                .build());
            resolver.applyBatch(ContactsContract.AUTHORITY, ops);

1 个答案:

答案 0 :(得分:0)

您需要明确将新$('#save').click(function() { $.ajax({ type: 'POST', contentType: 'application/json', url: 'save_categories.php', dataType: 'json', data: JSON.stringify({'categories': $('.dd').nestable('serialize')}), success: function(msg) { alert(msg); } }); });汇总到现有(原始)联系人中:

$json = json_decode(file_get_contents("php://input"));
$categories = $json->categories;

当然,您可以使用RawContact在创建新ops.add(ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI) .withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER) .withValue(AggregationExceptions.RAW_CONTACT_ID1, yourNewRawContactId) .withValue(AggregationExceptions.RAW_CONTACT_ID2, someExistingRawContactId) .build());

的同一批次中运行此操作