我能够获得一个通用通知“联系人数据库发生了变化”,但我想知道插入,更新或删除的特定记录。以下是注册并获取onChange通知的代码。不幸的是,它并不具体,这使得我的处理功能详尽且效率低下。
这是代码存根:
if ((mNativeContactsObserver == null) && (mHandler == null)) {
mHandler = new Handler(this.getMainLooper()) {
};
mNativeContactsObserver = new ContentObserver(mHandler) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Bundle data = null;
Message message = mHandler.obtainMessage();
if (message != null) {
data = message.getData();
if (data != null) {
Logs.d(TAG, "Message = [" + message.toString() + "] data=[" + data.toString() + "]");
Logs.d(TAG, "Contents = [" + message.describeContents() + "]");
}
}
if (!selfChange) {
final Account accountListen = MySyncAdapter.lookupAccount(TAG, getApplicationContext(), getUserProfile().getAccountId(), AUTHORITY_MY_SYNC);
Logs.d(TAG, "onChange!? account: " + accountListen.name);
if (!ContentResolver.isSyncPending(account, ContactsContract.AUTHORITY)
&& (!ContentResolver.isSyncActive(account, ContactsContract.AUTHORITY))) {
Bundle extras = new Bundle();
extras.putInt(MySyncAdapter.EXTRA_SYNC_TYPE, MySyncAdapter.REQUEST_SYNC_NATIVE_CHANGED);
ContentResolver.requestSync(accountListen, ContactsContract.AUTHORITY, extras);
} else {
Logs.w(TAG, "There is a pending sync. This request is ignored.");
}
}
}
};
}
Uri uriContactsListen = ContactsContract.Contacts.CONTENT_URI.buildUpon().appendEncodedPath("#").build();
Logs.i(TAG, "Register listening for native contacts changes. [" + uriContactsListen + "]");
cr.registerContentObserver(uriContactsListen, true, mNativeContactsObserver);
答案 0 :(得分:4)
最后至少我能够检测到更新/删除/插入查看我的OnChange
方法,如下所示
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.e("ContactChangeObserver", "onChange");
// 0 Update , 1 Delete , 2 Added
// Get count from phone contacts
final int currentCount = contactDBOperaion.getContactsCountFromPhone();
// Get count from your sqlite database
int mContactCount= DbContacts.getInstance().getContactsCount();
if (currentCount < mContactCount) {
// DELETE HAPPEN.
Log.e("Status", "Deletion");
contactDBOperaion.SyncContacts(1);
} else if (currentCount == mContactCount) {
// UPDATE HAPPEN.
contactDBOperaion.SyncContacts(0);
} else {
// INSERT HAPPEN.
Log.e("Status", "Insertion");
contactDBOperaion.SyncContacts(2);
}
}
发生更新时,我会更新所有联系人
发生插入时,我检查后插入新添加的行 isExist在现有数据库中
删除时未找到解决方案
答案 1 :(得分:3)
我的应用程序基类中有这段代码。
private ContentObserver contactObserver = new ContactObserver();
private class ContactObserver extends ContentObserver {
public ContactObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// Since onChange do not sent which user have been changed, you
// have to figure out how to match it with your data.
// Note: Contact is one of my classes.
for (Contact contact : getContacts()) {
if (!contact.isLinked())
continue;
String selection = ContactsContract.Data._ID + " = ?";
String[] selectionArgs = new String[] { contact.getSystemId() };
String[] projection = new String[] { ContactsContract.Data.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection,
selection, selectionArgs, null);
if (!cursor.moveToFirst())
return;
String name = cursor.getString(0);
if (contact.getUsername().equalsIgnoreCase(name))
continue;
contact.setUserName(name);
}
}
}
关于投射检查here
的内容Unfortunately, it is not specific which makes my processing exhaustive and inefficient.
如果进入下一个Android版本,也会发送刚刚更改过的联系人ID。
希望这有帮助
答案 2 :(得分:0)
首先,您必须注册contentObserver以接收更改通知。
通过致电:
来做到这一点registerContentObserver();
以下是规格:registerContetObserver
之后你会想要在修改时通知所有听众:
contentResolver.notifyChange();
以下是该规范的规格:notifyChange
希望它有所帮助;)
干杯!
答案 3 :(得分:-2)
使用加载程序在模拟器上查询并显示Android设备上的联系人列表。 它们会自动检测基础数据中的更改并在您的UI上反映出来。