1

I wanna delete ALL the messages in my Messaging with just one click on the button. But I already tried the coding below, it is not working... Can someone just help me to achieve this? Thanks...

public class DeleteSMSActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button press;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    press = (Button)findViewById(R.id.button1);
    press.setOnClickListener(this);
}

public void onClick(View view){

    ContentResolver cr = getContentResolver();
    Uri inboxUri = Uri.parse("content://sms/inbox");
    Cursor c = cr.query(inboxUri , null, null, null, null);
    while (c.moveToNext()) {
        // Delete the SMS
        String pid = c.getString(0); // Get id;
        String uri = "content://sms/" + pid;
        cr.delete(Uri.parse(uri), null, null);
    }
}
}

What should I add in Manifest? Having force close when testing with my Galaxy Tab 2

user1782267
  • 313
  • 4
  • 10
  • 20

4 Answers4

7

Try deleting with _id :

Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
try {
      while (c.moveToNext()) {
         int id = c.getInt(0);
         getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
      }

    }catch(Exception e){
         Log.e(this.toString(),"Error deleting sms",e);
    }finally {
      c.close();
    }
S.D.
  • 29,290
  • 3
  • 79
  • 130
0

The delete uri is "content://sms/" + id;

Uri inboxUri = Uri.parse("content://sms/inbox");
int count = 0;
Cursor c = context.getContentResolver().query(inboxUri , null, null, null, null);
while (c.moveToNext()) {
    try {
        // Delete the SMS
        String pid = c.getString(0); // Get id;
        String uri = "content://sms/" + pid;
        count = context.getContentResolver().delete(Uri.parse(uri),
                null, null);
    } catch (Exception e) {
    }
}
return count;

you can Also Refere HERE & HERE for More Detail Information About this topic. Hope it Will Help you.

Community
  • 1
  • 1
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

for Deleting SMS from inbox you need uses permission declared in your manifest

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

now you will be able to delete your inbox sms and now you wont get the force close your emulator

Ashfaque
  • 1,254
  • 1
  • 22
  • 38
0

Today I found the way to delete all messages without loop, just by query.

mContext.getContentResolver().delete(Uri.parse("content://sms/"), null, null);

Follow this link for more detail http://www.wisdomitsol.com/blog/android/sms/programmatically-delete-all-messages-in-android

Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44