我正在尝试在更改联系人表时提取联系人。它工作正常,但现在不起作用。它只工作几次。我正在使用 ContentObserver 来获取事件。
这是注册 ContentObserver 的代码。我在服务上注册它以不断收听。
public void onCreate() {
super.onCreate();
context = this;
db = new Database(context);
Log.i("**********", "Started Service!");
Contacts contacts = new Contacts(context);
context.getContentResolver().registerContentObserver (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, contacts);
}
这是我提取联系人姓名和此内容时的代码:
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("********","*****");
db = new Database(co);
db.open();
if (db.Preferences(3) && new CheckConexion().isOnline(co)) {
Cursor phones = co.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
while (phones.moveToNext()) {
try {
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
JSONObject jobject = db.select_contact(name, phoneNumber);
String type1 = jobject.getString("type");
if (type1.equals("upload")) {
if (!(name.equals("") && phoneNumber.equals(""))) {
JSONObject jobject2 = new JSONObject();
jobject2.put("name", name);
jobject2.put("phone", phoneNumber);
String value_json = String.valueOf(jobject2);
upload(value_json, co);
Log.i("********", "Upload on the db" + name + " "
+ phoneNumber);
}
} else {
return;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
db.close();
} else {
db.close();
}
}