gpt4 book ai didi

android - 如何让我的 Android ContentObserver for ContactsContract 检测到添加、更新或删除的联系人?

转载 作者:IT老高 更新时间:2023-10-28 21:49:35 26 4
gpt4 key购买 nike

我能够收到“联系人数据库发生更改”的一般通知,但我想知道插入、更新或删除的特定记录。以下是注册并获取 onChange 通知的代码。不幸的是,这不是具体的,这使我的处理详尽且效率低下。

这里是代码 stub :

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

最佳答案

最后至少我能够检测到这个 Update/Delete/Insert 看看我的 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);
}
}

When Update occurred i update all contacts

When Insert occurred i am inserting newly added row , after checking isExist in existing database

When Delete occurred not found solution yet

关于android - 如何让我的 Android ContentObserver for ContactsContract 检测到添加、更新或删除的联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5996921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com