有人能告诉我如何从Android手机上删除通话记录吗?
我正在使用以下代码行
getActivity().getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);
但是,这对我不起作用我想删除整个通话记录历史。
最佳答案
下面的代码对我有用…
private void deleteNumber() {
try {
String strNumberOne[] = { "00577698160" };
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
boolean bol = cursor.moveToFirst();
if (bol) {
do {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
} while (cursor.moveToNext());
}
} catch (Exception ex) {
System.out.print("Exception here ");
}
}
编辑:
下面的代码在api 23中也适用于我
确保您在manifast.xml中拥有以下权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
要删除特定号码的通话记录,请尝试以下方法:
public void DeleteCallLogByNumber(String number) {
String queryString="NUMBER="+number;
this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
}
}