广播有意接触添加/编辑/改变 - 机器人 [英] Broadcast Intent on contact added/edited/changed - android

查看:124
本文介绍了广播有意接触添加/编辑/改变 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :)

这篇关于广播有意接触添加/编辑/改变 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆