簡體   English   中英

Android Studio:刪除特定號碼發送和接收的短信

[英]Android Studio: Delete sent and recieved sms by particular number

我想知道如何刪除特定號碼發送和接收的短信。 我的應用程序正在與其他設備的 GSM 模塊連接,它發送和接收 SMS 消息。 我想刪除這些短信。 下面是我的代碼:

@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});
                }
            }
        }
    }

源代碼是捕獲 SMS 消息。 我對 where 子句有疑問。 刪除短信不起作用。

刪除-android-sms-programmatically中歸功於“Maksim Dmitriev”的答案

請注意,您無法在使用 Android 4.4 的設備上刪除 SMS 消息。

此外,系統現在只允許默認應用程序將消息數據寫入提供程序,盡管其他應用程序可以隨時讀取。

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

嘗試不會拋出異常; 什么都不會被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM