简体   繁体   中英

Android Studio: Delete sent and recieved sms by particular number

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM