我创建了联系人变更观察员,当用户随时改变他们的联系时需要知道他们做了什么改变。
第1步:
public class ContactChangeObserver extends ContentObserver {
Context mContext;
public ContactChangeObserver(Handler handler, Context ctx) {
super(handler);
mContext = ctx;
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.e("Content Change", "Added New onChange selfChange");
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.e("NoOf Content Change", "Contant Change Happen");
super.onChange(selfChange, uri);
if(mContext != null) {
Log.e("Content Change Name :", "start checking");
AppPreferences appPreferences = new AppPreferences(mContext);
String[] projection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
if (cursor.moveToFirst()) {
String sender = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String mobileno = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String status = "phone";
MaraContactsManager.getInstance().updateContactDB(mContext, mobileno, sender, status, appPreferences.getCountryCode());
}
}
}
第2步:通过调用HomeActivity
进行注册contactChangeObserver = new ContactChangeObserver(new Handler(),getApplicationContext());
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactChangeObserver);
第3步:当我更改任何联系时,它会转到 onChange Methode,但我没有得到完成的联系人更改
java.lang.IllegalArgumentException: URI: content://com.android.contacts, calling user:
请帮助