之前試過使用刪除最近一條通話記錄,發現會把手機裏面的記錄也給刪除,不知道是代碼寫的有問題仍是別的緣由:get
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI,
new String[] { "_id" }, null, null, "_id desc limit 1");
if (cursor.moveToFirst()) {
int id = cursor.getInt(0);
resolver.delete(CallLog.Calls.CONTENT_URI, " _id = ? ",
new String[] { id + "" });
return true;
}it
發如今使用廣播監聽的時候io
phone_state.equals(TelephonyManager.EXTRA_STATE_IDLE)會執行兩次im
致使打點都沒有通話記錄。static
最後換了一種不是很好的解決辦法:時間
結束電話的時候去掃描通話記錄,將其中的電話號碼的長度小於3的記錄刪除query
實現代碼:while
讀取全部的通話記錄(其實能夠給定一個時間限制,那樣會好一點)
co
ContentResolver resolver = context.getContentResolver();
Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, null);
if(cursor.moveToFirst()){
do{
String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
System.out.println("電話號碼:"+number);
if(number.length()<3){
deleteCallLogByNumber(context, number);
return true;
}
//刪除私人電話的通話記錄
}while(cursor.moveToNext());
}
刪除指定號碼的通話記錄:let
public static boolean deleteCallLogByNumber(Context context, String phonenumber) { boolean ret = false; ContentResolver cr = context.getContentResolver(); try { if (phonenumber != null && phonenumber.length() > 0) { int i = cr.delete(CallLog.Calls.CONTENT_URI, "number=?", new String[] { phonenumber }); Log.d("xx", "i:" + i); if (i > 0) { ret = true; } } } catch (Exception e) { e.printStackTrace(); } return ret; }