我想知道是否有辦法知道呼叫的內容提供者是否發生了變化。我的意思是,如果我撥打電話,或者我接聽電話,它會返回一個「標誌」,表示已將新日誌添加到通話記錄或Android存儲有關通話的信息的位置。如何實現呼叫日誌的ContentObserver
因爲,當我撥打電話時,Android會在內容提供商中存儲號碼,聯繫人姓名(如果存在),通話時間,持續時間......。那麼是否有一種方法可以捕獲這個「標誌」,表示內容提供者的調用更大,我的意思是一個新的數據已經被插入到內容提供者CallLog.Calls中。
因此,我仍然對這個問題有很多疑問。我不知道在哪裏註冊內容觀察員。我的意圖是,當CallLog內容提供者發生變化時,將使用代碼的插入方法。
我的意思是,除非已將新數據添加到CallLog內容提供程序,否則代碼將不會執行任何操作。如果某些數據已添加到CallLog內容提供程序中,則代碼將查詢新數據,然後插入。我想這樣做,因爲沒有內容觀察者,應用程序在數據庫中插入數據,每次運行應用程序時都會插入數據,知道了嗎?
所以這裏是我的代碼。如果有人可以告訴我在哪裏放registerContentObserver()
和其他所有需要的東西。
public class RatedCalls extends ListActivity {
private SQLiteDatabase db;
private CallDataHelper dh = null;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DATE + " DESC ");
dh = new CallDataHelper(this);
db = openHelper.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("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", callDate);
values.put("current_time", currTime);
values.put("cont", 1);
this.db.insert(CallDataHelper.TABLE_NAME, null, values);
Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
callList.add("Contact Number: " + contactNumber
+ "\nContact Name: " + contactName + "\nDuration: "
+ duration + "\nDate: " + callDate);
} while (cursor.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, callList));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
不要忘記註銷在的onDestroy()方法的供應商。 – moonlightcheese 2011-03-11 15:35:58
如果你不這樣做會怎麼樣?我發現爲了取消註冊觀察者,我必須將其保存在記憶中,而我不想這麼做。 – 2011-04-05 06:35:56
在行 new RatedCallsContentObserver(handler)); 處理程序來自哪裏/爲什麼我需要這個?謝謝! – 2011-05-31 17:14:17