广播有意接触添加/编辑/改变 - 机器人机器人、编辑

2023-09-05 05:03:49 作者:姜生凉生

在我的应用程序,我有,我要听在联系人即当任何联系人添加或编辑在当地的联系人应用程序或通过任何其他应用程序的任何变化,服务。是否有广播的意图,我可以注册为这一目的?还是有实现这一目标的任何其他方式?

In my app, I have a service in which I want to listen for any changes in contacts i.e., when any contact is added or edited in the native contacts app or through any other app. Is there any broadcast intent which I can register to for this purpose? Or is there any other way to achieve this?

推荐答案

更​​好的替代解决方案在这里,

Better alternate solution here,

添加以下的的Manifest.xml权限

add following permission in Manifest.xml

<uses-permission android:name="android.permission.WRITE_CONTACTS" />

然后

private void saveContact(String name, String number){
                /**Code to save the contact*/
                ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>(); 
                op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
                        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
                        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
                        .build()); 

                op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.FAMILY_NAME, ""+name) 
                        .build()); 

                op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
                        .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, ""+number)
                        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_MOBILE)
                        .build());
                try{ 
                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list); 
                }catch(Exception e){ 
                    e.printStackTrace(); 
                } 

            }

CHEERS:)

CHEERS :)