Java 数据库上的服务插入超出预期
我正在从CallLog获取联系人,并使用带有ContentObserver的服务将其插入数据库。因此,ContentObserver将继续监视调用日志是否发生了一些新的更改,如果是,则在方法onChange中调用在调用日志中获取数据并将其插入数据库的方法 但是,问题是,如果我进行一个新调用,有一个新数据,因此,它必须插入数据库中,只有一个新行,但它插入了3行。我不知道为什么。我在安卓2.3模拟器上测试了它,在模拟器上它还可以,但在真正的2.3设备上它会发生。在2.1模拟器中,这种情况也会发生 这是我的代码。该服务已发布。谢谢 下面是MyApplicationService.java服务Java 数据库上的服务插入超出预期,java,android,android-service,android-sdk-2.3,android-2.1-eclair,Java,Android,Android Service,Android Sdk 2.3,Android 2.1 Eclair,我正在从CallLog获取联系人,并使用带有ContentObserver的服务将其插入数据库。因此,ContentObserver将继续监视调用日志是否发生了一些新的更改,如果是,则在方法onChange中调用在调用日志中获取数据并将其插入数据库的方法 但是,问题是,如果我进行一个新调用,有一个新数据,因此,它必须插入数据库中,只有一个新行,但它插入了3行。我不知道为什么。我在安卓2.3模拟器上测试了它,在模拟器上它还可以,但在真正的2.3设备上它会发生。在2.1模拟器中,这种情况也会发生 这
我不确定,但我怀疑可能涉及多个表,这就是为什么即使专门使用CallLog.Calls.CONTENT_URI,也会得到多个更新。消息来源会很快回答这个问题。我也不知道为什么仿真器的行为会有所不同,但我不同意他们的说法——不幸的是,他们有很多怪癖。无论如何,您是否尝试过将registerForContentObserver中的notifyForDescendants设置为FALSE,或者其他项是传递子体调用?我已将notifyForDescendants的值设置为FALSE,但它不起作用。还有一件事正在发生。每次我去CallLog活动,它都会得到我打电话的最后一个联系人,并将其插入数据库,即使我没有打电话给他。有点奇怪。它只发生在2.1上=/
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));
}
}