我从CallLog获取联系人,并使用带有ContentObserver的服务将其插入数据库。因此,ContentObserver会一直监视CallLog是否发生了一些新的更改,如果是,则在onChange()方法内部调用获取CallLog数据的方法并将其插入数据库。
但问题是,如果我进行一个新的调用,则有一个新数据,因此,它必须插入数据库中,只有一个新行,但它会插入3行。我不知道为什么。我在Android 2.3模拟器上进行了测试,在模拟器上测试它是可以的,但在真正的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));
}
}