我必须观察何时添加或更新任何联系人。新的联系人出现在表的末尾,所以使用 curser.moveToLast(); 我可以从观察者内部的联系人表中获得新添加的联系人但是如果我更新任何联系人如何获得该联系人的ID。请告诉我可能的方法。
答案 0 :(得分:0)
最后我有办法。如果有更好的方法,请告诉我。
此类将观察联系人表中的每个添加/更新条目: -
public class ObserverReceiver extends ContentObserver {
private static final String TAG = "ObserverReceiver";
private Context context;
public ObserverReceiver(Handler handler, Context context) {
super(handler);
this.context = context;
}
// will be called when database get change
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d(TAG, "Observer has been started..");
long currentTimeStamp = System.currentTimeMillis();
// Contacts Cursor
Cursor cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Check which contact is added or updated
if (cur != null) {
while (cur.moveToNext()) {
// Get contact added/updated timestamp but CONTACT_LAST_UPDATED_TIMESTAMP
//is added in API 18
String timeStampStr = cur
.getString(cur.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));
long timeStamp = Long.parseLong(timeStampStr);
// Check if any contact id added/updated with in 5 second
// you can reduce the time it take hardly 1 or 2 sec in
// processing so i hape you will get the exact entry every time
if (timeStamp > (currentTimeStamp - 5000))
// Get new/updated contact detail here
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
cur.close();
}
}
:)