Delete sms from the received message tray

0

Good morning everyone,

I'm doing an app in Android Studio that exchanges SMS, but I would like it to read the message and automatically delete it in the SMS tray received.

I use the following code but it does not work for me:

Cursor cur = context.getContentResolver().query(Uri.parse("content:
//sms/inbox"), null, null, null, null);
if (cur.moveToFirst()) {
do {
int indexBody = cur.getColumnIndex("body");
int indexId = cur.getColumnIndex("_id");
if (mensaje.equals(cur.getString(indexBody))) {
String uri = "content://sms/inbox" + cur.getInt(indexId); 
                         context.getContentResolver().delete(Uri.parse(uri), null, null);
                    }
                } while (cur.moveToNext());
            }

The message variable read it correctly before.

Also, I have all the permissions in the manifest and accepted in the mobile.

You know I can be doing wrong.

    
asked by javier 24.01.2018 в 17:12
source

1 answer

0

Android 4.4 Kitkat introduced changes to SMS handling where now only the default SMS application has write access to the SMS database; all other applications fail silently when they try to write.

I used this code recently to solve something similar, of course the app must be selected as default SMS application on the device as long as it has android 4.4 or higher.

Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);
    try {
          while (c.moveToNext()) {
             int Id = c.getInt(0);
             String pid = c.getString(0);
            // String uri = "content://sms/conversations/" + threadId;
             String strUriAll = "content://sms/" + pid;//SMS_ALL
             Log.d("URI is ", strUriAll);
             getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
          //   getApplicationContext().getContentResolver().delete(Uri.parse(strUriAll), null, null);
          }

        }catch(Exception e){
             Log.e(this.toString(),"Error deleting sms",e);
        }finally {
          c.close();
        }
    
answered by 24.01.2018 / 18:20
source