How to delete call log on Android

Every day, you make calls, get incoming and missed calls. The calls data are stored in your phone and can be viewed by selecting Logs. To free up your phone's pace, these call logs information should be removed especially the old calls information. In Android tip, I am going to show you how to delete call logs of Android devices programmatically.


An Android phone stores outgoing, incoming, and missed calls in a table called CallLog.Calls.CONTENT_URI. Before you are going to delete call logs, you should know the brief structure of the table. This table contains the following fields:
-DATE: the date the call was made.
-DURATION: the duration of the call in seconds.
-TYPE: the type of the call (incoming, outgoing or missed).
-NEW: whether or not the call has been acknowledged.
-CACHED_NAME: The cached name associated with the phone number, if it exists.
-CACHED_NUMBER_TYPE: the cached number type (Home, Work, etc) associated with the phone number, if it exists
-CACHED_NUMBER_LABEL: the cached number label, for a custom number type, associated with the phone number, if it exists.
-_COUNT: the number of rows in a directory.
-_ID: the unique id of the row.

To delete call logs, you will use the delete(Uri url, String where, String whereArgs) method of the ContentResolver class. For the url argument, you will need CallLog.Calls.CONTENT_URI. The where is the WHERE clause to apply when deleting the rows. You can use ? symbols in where clause and replace them later in the whereArgs argument.

Now to have a workable example application that can be used to delete call logs, create a new Android project. Then modify the activity_main.xml file to add a Button as shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <Button
     android:id="@+id/bt_delete"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Delete old call logs"
     android:layout_centerInParent="true"
     android:onClick="deleteCallLogs"
      />

</RelativeLayout>

In the MainActivity class, you will add a new method. The name of the method is deleteCallLogs. It will be called when you click the Button to delete the call logs. In this example, the calls that are older than the date seven day backed from the current date will be deleted.

public void deleteCallLogs(View view){


//get ContentResolver
ContentResolver cr=getContentResolver();
//Where
String sel =CallLog.Calls.DATE + " <?";
//get the date backed to the last 7 days
Calendar cal = Calendar.getInstance();  
cal.add(Calendar.DAY_OF_MONTH, -7);
Date dt=cal.getTime();
//Selection args
String[] selArgs={String.valueOf(dt.getTime())};
//delete old call logs
int num_deleted=cr.delete(CallLog.Calls.CONTENT_URI, sel,selArgs);
Toast.makeText(this, num_deleted+" items are deleted.", Toast.LENGTH_SHORT).show();

}

Before running the example application, you must add the permission to read and write call logs to the AndroidManifest.xml file.

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

How-to-delete-call-log-on-Android

Posted by: Dara | post date: 08-05-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.