How to remove call logs from android programmatically?

做~自己de王妃 提交于 2019-11-30 14:53:00

问题


how to delete/remove call log from application. I am doing like this

 this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,null,null);

it not working.


回答1:


Make sure u have following permissions in Manifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

For deleting call logs for particular number try this way:

public void DeleteCallLogByNumber(String number) {   
    String queryString = "NUMBER=" + number; 
    this.getContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null);  
}



回答2:


The existing solution will not delete numbers with 0 or + prefix. For this to work for all phone numbers, one needs to put the number in single quotes, like so:

String queryString = "NUMBER='"+numberToDelete+"'";
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null);

Hope this helps.




回答3:


Accepted answer will delete all calls from call log for a specific number. If you want to delete a only single call you can do it by passing CallLogId to that function and run this query.

public void DeleteCallById(String idd) {   
    this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,CallLog.Calls._ID + " = ? ",
            new String[] { String.valueOf(idd) });
    }  



回答4:


<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

You need to give only this permission to work along with this method:

this.getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);

Its working perfectly for me. I've tested it on my Moto-G running Kitkat 4.4.2 and Samsung Note with Jelly Bean 4.1.




回答5:


Here is an improved way, for example if the stored number in database is like:"914111222" this method can deal with numbers like:"+98 914 111 2222":

public void removeContactsLogFromPhoneLogs(String numberTag){
    char[] number=numberTag.toCharArray();
    String n="%";
    for(int i=0;i<number.length;i++)
    {
        n=n+(number[i]+"%");
    }
    String queryString=CallLog.Calls.NUMBER+" LIKE '"+n+"'"; 
    mContext.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);

}

it requires permission as:

<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>


来源:https://stackoverflow.com/questions/10947283/how-to-remove-call-logs-from-android-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!