How to remove call logs from android programmatica

2019-01-25 09:13发布

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.

5条回答
SAY GOODBYE
2楼-- · 2019-01-25 09:40

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) });
    }  
查看更多
劫难
3楼-- · 2019-01-25 09:40

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"/>
查看更多
不美不萌又怎样
4楼-- · 2019-01-25 09:47
<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.

查看更多
Juvenile、少年°
5楼-- · 2019-01-25 09:52

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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-25 09:54

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);  
}
查看更多
登录 后发表回答