在一个 android 应用程序中,我想获取我正在使用内容观察器的通话记录。单击按钮时,我想查看通话记录。但是当我第一次运行这个程序并在模拟器上进行调用时,记录被成功添加并显示在列表视图中。但是当我再次拨打电话时,最新记录被插入两次,类似地,listview 显示 2 次最新记录和 1 次旧记录。
请指导我哪里出错了?真的需要尽快完成。谢谢!
package com.callloglist;
import static com.callloglist.Constants.KEY_ContactName;
import static com.callloglist.Constants.KEY_ContactNum;
import static com.callloglist.Constants.KEY_CurrTime;
import static com.callloglist.Constants.KEY_Date;
import static com.callloglist.Constants.KEY_Duration;
import static com.callloglist.Constants.KEY_NumType;
import static com.callloglist.Constants.TABLE_NAME;
import java.text.SimpleDateFormat;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private Helper helper = new Helper(this);
private SimpleCursorAdapter dataAdapter;
private ListView listview;
SQLiteDatabase db;
@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()));
Button Btn = (Button) findViewById(R.id.button1);
Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, KEY_Date + " DESC");
final String[] columns = new String[]{KEY_ContactName,KEY_Date,KEY_Duration,};
final int[] to = new int[]{R.id.textView1,R.id.textView2,R.id.textView3};
dataAdapter= new SimpleCursorAdapter(getApplicationContext(), R.layout.log_layout, c, columns, to);
listview = (ListView) findViewById(R.id.list_view1);
// Assign adapter to ListView11
listview.setAdapter(dataAdapter);
}
});
}
public void addLog() {
db = helper.getWritableDatabase();
Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
cursor.moveToFirst();
// TODO Auto-generated method stub
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;
String contactNumber = cursor.getString(numberColumnId);
String contactName = cursor.getString(contactNameId);
String duration = cursor.getString(durationId);
String callDate = cursor.getString(dateId);
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
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, dateString);
values.put(KEY_CurrTime, currTime);
db.insert(TABLE_NAME, null, values);
cursor.close();
db.close();
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();
}
}
}