我从 CallLog 中获取联系人,并使用带有 ContentObserver 的服务将其插入数据库。因此,ContentObserver 会继续观察 CallLog 是否发生了一些新变化,如果是,则在 onChange() 方法中调用获取 CallLog 中的数据并将其插入数据库的方法。
但是,问题是,如果我打一个新的电话,有一个新数据,所以,它必须插入到数据库中,只有一个新行,但它插入了 3 行。我不知道为什么。我在 Android 2.3 Emulator 上对其进行了测试,在模拟器上它还可以,但在真正的 2.3 设备上它会发生。在 2.1 模拟器中它也会发生。
所以这是我的代码。该服务已发布。谢谢!
这是服务MyApplicationService.java
public class MyApplicationService extends Service {
private Handler handler = new Handler();
private SQLiteDatabase db;
private OpenHelper helper;
class MyApplicationContentObserver extends ContentObserver {
public MyApplicationContentObserver(Handler h) {
super(h);
helper = new OpenHelper(getApplicationContext());
db = helper.getWritableDatabase();
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
searchInsert();
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
db = DataHandlerDB.createDB(this);
registerContentObservers();
}
@Override
public void onDestroy() {
db.close();
}
private void searchInsert() {
Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
if (cursor.moveToFirst()) {
int numberColumnId = cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int durationId = cursor
.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int contactNameId = cursor
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int numTypeId = cursor
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
int callTypeId = cursor
.getColumnIndex(android.provider.CallLog.Calls.TYPE);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String currTime = hours + ":" + minutes + ":" + seconds;
SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");
Date date = new Date();
cursor.moveToFirst();
String contactNumber = cursor.getString(numberColumnId);
String contactName = (null == cursor.getString(contactNameId) ? ""
: cursor.getString(contactNameId));
String duration = cursor.getString(durationId);
String numType = cursor.getString(numTypeId);
String callType = cursor.getString(callTypeId);
ContentValues values = new ContentValues();
values.put("contact_id", 1);
values.put("contact_name", contactName);
values.put("number_type", numType);
values.put("contact_number", contactNumber);
values.put("duration", duration);
values.put("date", dateFormat.format(date));
values.put("current_time", currTime);
values.put("cont", 1);
values.put("type", callType);
if (!db.isOpen()) {
db = getApplicationContext()
.openOrCreateDatabase(
"/data/data/com.myapp.app/databases/my_database.db",
SQLiteDatabase.OPEN_READWRITE, null);
}
db.insert(DataHandlerDB.TABLE_NAME_2, null, values);
cursor.close();
}
}
public void registerContentObservers() {
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(
android.provider.CallLog.Calls.CONTENT_URI, true,
new MyApplicationContentObserver(handler));
}
}