0

I'd like to know how to delete sent and received SMS by a particular number. My app is connecting with the GSM module with other device and it sends and receives SMS messages. I'd like to delete these SMS. Below is my code:

@Override
    public void onReceive(Context context, Intent intent) {
        if (Objects.equals(intent.getAction(), SMS_RECEIVED)) {
            String smsSender = "";
            String smsBody = "";
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                StringBuilder smsBodyBuilder = new StringBuilder();
                for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                    smsSender = smsMessage.getDisplayOriginatingAddress();
                    smsBodyBuilder.append(smsMessage.getMessageBody());
                }
                smsBody = smsBodyBuilder.toString();
            } else {
                Bundle smsBundle = intent.getExtras();
                if (smsBundle != null) {
                    Object[] pdus = (Object[]) smsBundle.get("pdus");
                    if (pdus == null) {
                        // Display some error to the user
                        return;
                    }
                    SmsMessage[] messages = new SmsMessage[pdus.length];
                    StringBuilder smsBodyBuilder = new StringBuilder();
                    for (int i = 0; i < messages.length; i++) {
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        smsBodyBuilder.append(messages[i].getMessageBody());
                    }
                    smsBody = smsBodyBuilder.toString();
                    smsSender = messages[0].getOriginatingAddress();
                }
            }

            if (smsSender != null) {
                if (smsSender.equals(serviceProviderNumber) && smsBody.startsWith(serviceProviderSmsCondition)) {
                    this.pdCanceller.removeCallbacks(this.progressRunnable);
                    this.message = smsBody;
                    context.unregisterReceiver(broadcastReceiver);
                    progressDialog.cancel();
                    SmsReceiverDialog smsReceiverDialog = new SmsReceiverDialog(this.activity, this.context, this.message);

                    checkCommand(smsBody, smsReceiverDialog); // call correctly function from list
                    context.getContentResolver().delete(Uri.parse("content://sms/"), "address=?", new String[]{smsSender});
                }
            }
        }
    }

The source code is catching SMS messages. I have a problem with where clause. Deleting SMS doesn't work.

MurugananthamS
  • 2,339
  • 4
  • 18
  • 48
response
  • 17
  • 6

1 Answers1

0

Credits to the answer of "Maksim Dmitriev" from deleting-android-sms-programmatically

Please consider that you can't delete SMS messages on devices with Android 4.4.

Also, the system now allows only the default app to write message data to the provider, although other apps can read at any time.

http://developer.android.com/about/versions/kitkat.html

No exception will be thrown if you try; nothing will be deleted.

Osama A.Rehman
  • 862
  • 1
  • 5
  • 11
  • Yes, I know that I can't delete sms on KitKat. My minSDK is 22, Android 4.4 isn't my target. To recap, I can't delete sms on any version of Android? – response Oct 18 '19 at 06:37