Delete SMS Android

In the previous post you learnt how to delete old call logs from your Android device. In Android tip, I am going to show you how to delete all SMS from your Android devices programmatically.

An Android phone stores SMS in a table of SQLite database. You can refer to the table by creating an Uri object with its parse method:

Uri url=Uri.parse("content://sms/");

There are 17 columns in the table. If you want to get all column names of the table, the following code can help you:

ContentResolver cr=getContentResolver();
Uri url=Uri.parse("content://sms/");
Cursor cursor=cr.query(url, null,null,null,null);
int col_nums=cursor.getColumnCount();
for(int i=0;i<col_nums;i++){
Log.e("Column"+i,cursor.getColumnName(i));

}


To delete all SMS that are stored in your phones, you use the delete(Uri url, String where, String whereArgs) method of the ContentResolver class. For the url argument, you will need the url object created above. You can put null values for the where and whereArgs arguments as you want to delete all SMS.

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. The Button is clicked to delete SMS.

<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 SMS"
     android:layout_centerInParent="true"
     android:onClick="deleteSMS"
      />

</RelativeLayout>

In the MainActivity class, you will add a new method. The name of the method is deleteSMS. It will be called when you click the Button to delete SMS stored on the phone.

public void deleteSMS(View view){
ContentResolver cr=getContentResolver();
Uri url=Uri.parse("content://sms/");
int num_deleted=cr.delete(url, null, null);
Toast.makeText(this, num_deleted+" items are deleted.", Toast.LENGTH_SHORT).show();

}

Before running the example application, you must add the permission to read from and write to SMS database to the AndroidManifest.xml file.

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

Delete-SMS-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.