简体   繁体   中英

How to remove call logs from android programmatically?

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.

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);  
}

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.

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) });
    }  
<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.

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"/>

If you want to delete multiple call log by specific ids you can use the method below:

public void deleteCallLog(List<String> listOfIds){
    getContentResolver().delete(CallLog.Calls.CONTENT_URI,CallLog.Calls._ID + " in (" + TextUtils.join(",", listOfIds) + ")",
            null);
    callLogAdapter.notifyDataSetChanged();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM