我想实现一个程序,每次拨打电话或发送短信时都会在数据库中插入详细信息。我应该使用广播接收器或内容观察者或服务吗?什么是合适的?我是 android 新手,急需这方面的帮助。
到目前为止,我已经完成了以下代码。这样做的问题是,当第一次运行代码时,例如调用日志有 8 条记录,因此这 8 条记录被插入到数据库中。然后,如果通话记录发生任何变化,例如我打了另一个电话,那么数据库中会显示 17 条记录而不是 9 条。请帮助我哪里出错了?
package com.calllogdb;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
//import static android.provider.BaseColumns._ID;
import static com.calllogdb.Constants.KEY_ContactNum ;
import static com.calllogdb.Constants.KEY_ContactName;
import static com.calllogdb.Constants.KEY_Duration;
import static com.calllogdb.Constants.KEY_Date ;
import static com.calllogdb.Constants.KEY_NumType ;
import static com.calllogdb.Constants.KEY_CurrTime ;
import static com.calllogdb.Constants.TABLE_NAME;
public class MainActivity extends Activity {
private Helper helper = new Helper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(
android.provider.CallLog.Calls.CONTENT_URI, true,
new CallLogObserver(new Handler()));
try {
addLog();
}
finally {
helper.close();
}
}
private void addLog() {
// TODO Auto-generated method stub
SQLiteDatabase db;
Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
db = helper.getWritableDatabase();
startManagingCursor(cursor);
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 dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String currTime = hours + ":" + minutes + ":" + seconds;
ArrayList<String> callList = new ArrayList<String>();
if (cursor.moveToFirst()) {
do {
String contactNumber = cursor.getString(numberColumnId);
String contactName = cursor.getString(contactNameId);
String duration = cursor.getString(durationId);
String callDate = DateFormat.getDateInstance().format(dateId);
String numType = cursor.getString(numTypeId);
ContentValues values = new ContentValues();
values.put(KEY_ContactName, contactName);
values.put(KEY_NumType, numType);
values.put(KEY_ContactNum, contactNumber);
values.put(KEY_Duration, duration);
values.put(KEY_Date, callDate);
values.put(KEY_CurrTime, currTime);
db.insert(TABLE_NAME, null, values);
callList.add("Contact Number: " + contactNumber
+ "\nContact Name: " + contactName + "\nDuration: "
+ duration + "\nDate: " + callDate);
} while (cursor.moveToNext());
}
Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class CallLogObserver extends ContentObserver
{
public CallLogObserver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
@Override
public void onChange(boolean selfChange) {
Log.d("LOG_TAG", "MyContentObserver.onChange( " + selfChange
+ ")");
super.onChange(selfChange);
addLog();
}
}
}
每当我拨打电话时,它都会收到通知并插入整套记录,而不仅仅是最新的记录。我应该怎么做才能防止这种情况发生?