有很多与它相关的问题,但没有一个能帮助我找到解决方案。
我正在尝试同步所有联系人从设备到远程服务器,并能够轻松地做到这一点,但当有一个像更新/删除/插入(新联系人)联系方式的变化,无法找到解决方案。
已尝试使用ContentObserver
,但多次调用onChange()
。很难找到联系人变更数据。
public class ContactService extends Service {
private int mContactCount;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mContactCount = getContactCount();
Log.d("Contact Service", mContactCount + "");
this.getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, mObserver);
}
private int getContactCount() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
if (cursor != null) {
return cursor.getCount();
} else {
return 0;
}
} catch (Exception ignore) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return 0;
}
private ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
final int currentCount = getContactCount();
if (currentCount < mContactCount) {
// CONTACT DELETED.
Log.d("Contact Service", currentCount + "");
} else if (currentCount == mContactCount) {
// CONTACT UPDATED.
og.d("Contact Service", currentCount+"");
} else {
// NEW CONTACT.
Log.d("Contact Service", currentCount + "");
}
mContactCount = currentCount;
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(mObserver);
}
}
但是当有更新/插入到地址簿中时,onChange()
会被多次调用。
有人能为我提供更好的解决方案吗?
我将不胜感激。
谢谢
发布于 2017-06-25 02:52:39
根据Magnus的回答,使用此列检索版本代码
ContactsContract.RawContacts.VERSION
发布于 2021-01-29 10:41:02
这对我来说工作得很好。
1-注册服务
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true,
new ContactsListner(new Handler(),this,getContactCount()));
2- getContactCount()的定义
private int getContactCount() {
Cursor cursor = null;
try {
cursor = activity.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
if (cursor != null) {
return cursor.getCount();
} else {
return 0;
}
} catch (Exception ignore) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return 0;
}
3- Contact listner类
import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.ContactsContract;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class ContactsListner extends ContentObserver {
/**
* Creates a content observer.
*
* @param handler The handler to run {@link #onChange} on, or null if none.
*
*
*/
Activity activity;
int contactsCount;
public ContactsListner(Handler handler, Activity activity, int contactsCount) {
super(handler);
this.activity = activity;
this.contactsCount = contactsCount;
}
@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
super.onChange(selfChange, uri);
int currentContacts = getContactCount();
Toast.makeText(activity, "Current num is "+currentContacts+" and recent : "+contactsCount, Toast.LENGTH_SHORT).show();
if (currentContacts<contactsCount){
Toast.makeText(activity, "Deleted", Toast.LENGTH_SHORT).show();
}else if (currentContacts == contactsCount){
Toast.makeText(activity, "Update occurred", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(activity, "New contact added", Toast.LENGTH_SHORT).show();
}
contactsCount = currentContacts;
}
private int getContactCount() {
Cursor cursor = null;
try {
cursor = activity.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
if (cursor != null) {
return cursor.getCount();
} else {
return 0;
}
} catch (Exception ignore) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return 0;
}
}
https://stackoverflow.com/questions/21650621
复制相似问题