联系人列表中是否有添加/编辑/删除事件?
答案 0 :(得分:1)
欢迎您从ContentObserver
Uri
注册ContactsContract
ContentProvider
个Uri
,了解对{{1}}的更改。
答案 1 :(得分:0)
//initially all your contact is stored in database then use this service and regiter this service in manifest
public class ContactSync_Service extends IntentService {
public ContactSync_Service() {
super("intent service");
}
@Override
protected void onHandleIntent(Intent intent) {
MyContentObserver contentObserver = new MyContentObserver();
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(ContactsContract.Contacts.CONTENT_URI,
true, contentObserver);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
class MyContentObserver extends ContentObserver {
public MyContentObserver() {
super(null);
}
//whenever a contact is add,modified,delete this method will get called
@Override
public void onChange(boolean selfChange) {
Log.e("changes", "changes");
syncDatabase();
super.onChange(selfChange);
}
}
//comparing database contact list with phone's contact list if there is contact is added or modified
@TargetApi(18)
@SuppressWarnings("unused")
public void syncDatabase() {
ContactDatabase db = new ContactDatabase(this);
//get all the data from database include contact and name
//contact bean is a class containing setter and getter of getName() and getContact()
ArrayList<ContactBean> arrayListDataase = db.fetch();
int size = arrayListDataase.size();
Log.e("size of arraylist", size + "");
/*
* ArrayList<String> arrayListContact=new ArrayList<String>();
*
*
* for(int i=0;i<arrayListDataase.size();i++) {
* arrayListContact.add(arrayListDataase.get(i).getContact()); }
*/
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
int i = 0;
if (phones.moveToFirst()) {
do {
boolean flag = false;
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Log.e("number", phoneNumber);
for (i = 0; i < size; i++)
{
//comparing database contact number with phone's contact list contact number
if (arrayListDataase.get(i).getContact().equalsIgnoreCase(phoneNumber))
{
flag = true;
String nameFromContactList = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String nameFromDatabase=arrayListDataase.get(i).getName();
//check if name is changed in contact list
if(!nameFromContactList.equalsIgnoreCase(nameFromDatabase))
{
db.upDateNameWhenChangeInContactListOfPhone(nameFromContactList, phoneNumber);
}
break;
}
}
if (!flag) {
String image_uri="";
Log.e("contact changed", phoneNumber);
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String email = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String time_stamp = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP));
image_uri = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if((image_uri==null) || image_uri.matches("") )
{
image_uri="";
}
db.insert(name, phoneNumber, email, image_uri);
//break;
}
} while (phones.moveToNext());
phones.close();
}
}}