0

I'm trying to develop app in which my requirement is to delete sms from particular no. both outgoing and incoming message. right now i coded for incoming SMS as below. code is running fine to delete message but messages are not getting deleted in real.

this is for incoming messages only.

also please tell me how to delete outgoing messages also.

void deleteMessage(Context context){

    String no="55778866";
    try {

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(
                uriSms,
                new String[] { "_id", "thread_id", "address", "person",
                        "date", "body" }, "read=0", null, null);
        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);

                String address = c.getString(2);


                long threadId = c.getLong(1);
                String body = c.getString(5);
                String date = c.getString(3);
                Log.e("log>>>",
                        "0--->" + c.getString(0) + "1---->" + c.getString(1)
                                + "2---->" + c.getString(2) + "3--->"
                                + c.getString(3) + "4----->" + c.getString(4)
                                + "5---->" + c.getString(5));


                Log.e("log>>>", "date" + c.getString(0));

                ContentValues values = new ContentValues();
                values.put("read", true);
                getContentResolver().update(Uri.parse("content://sms/"),
                        values, "_id=" + id, null);

                Log.e("SMS Match >>>>","address >>>>"+address+" no. >>>>"+no+" match >>>> "+address.equals(no));

                if (address.equals(no)) {

                    try{
                        context.getContentResolver().delete(
                                Uri.parse("content://sms/" + id), "date=?",
                                new String[] { c.getString(4) });
                       Log.e("log>>>", "Delete success.........");
                       Toast.makeText(getApplicationContext(),"SMS Deleted",Toast.LENGTH_SHORT).show();

                    }catch (Exception e){
                        Log.e("DELETE >>>",e.getMessage());
                    }
                     }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Log.e("log>>>", "ERROR "+e.toString());
    }

}
RBT
  • 24,161
  • 21
  • 159
  • 240
imran khan
  • 382
  • 3
  • 15
  • 1
    From Android Kitkat I believe you cannot remove messages that way. I have a project where we use sms for silent communication and remove the sms directly after arrival and it works nicely on old devices. Sms arrives, we read it and delete before user notices. On modern devices sms is never deleted. Basically only primary messaging app can delete messages. – mhenryk May 26 '17 at 09:41
  • Thanks for your response, so if i add feature as to set my app as a default messaging app then will it work? – imran khan May 26 '17 at 10:12
  • Yes, but only if the user sets your app as the default messaging app, and I doubt they will do that just to allow your app to delete a few messages. A proper default messaging app should be a full SMS/MMS client. – Mike M. May 26 '17 at 10:26
  • Thanks! I will try this, I'm creating app for banking purpose so it is not for general user. – imran khan May 26 '17 at 11:54
  • If you just need your app to be eligible to be selected as the default, [this post](https://stackoverflow.com/a/30133663) might be of some help to you. – Mike M. May 26 '17 at 13:11

0 Answers0