android - 如何删除 Android 5.0 版 lollipop 或 Kitkat 中的特定收件箱消息?

标签 android sms android-4.4-kitkat android-5.1.1-lollipop

我正在执行删除电话号码任务的特定短信。当我在 motog 或 Android 5.0 版的手机上进行测试时。我无法删除特定号码的短信。我的代码 fragment 如下。

public void deleteSMS(Context context,String number) {
    try {
        Log.d("","Deleting SMS from inbox");
        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms,
                new String[] { "_id", "thread_id", "address",
                        "person", "date", "body" }, "address = '"+number+"'", null, null);

        if (c != null && c.moveToFirst()) {
            do {
                long id = c.getLong(0);
                long threadId = c.getLong(1);
                String address = c.getString(2);
                String body = c.getString(5);
                Toast.makeText(getApplicationContext(),"SMS with id: " + threadId +" Number:- " +address,Toast.LENGTH_LONG).show();
                Log.d("", "SMS with id: " + threadId +" Number:- " +address);
                if ( address.equals(number)) {
                    Log.d("", "Deleting SMS with id: " + threadId);
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), null, null);
                }
            } while (c.moveToNext());
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),"Could not delete SMS from inbox ",Toast.LENGTH_LONG).show();
        Log.e("", "Could not delete SMS from inbox: " + e.getMessage());
    }
}

最佳答案

在 4.4 之后,您不能从收件箱中删除任何短信,除非您的应用是“默认短信应用”

Beginning with Android 4.4, the system settings allow users to select a "default SMS app." Once selected, only the default SMS app is able to write to the SMS Provider and only the default SMS app receives the SMS_DELIVER_ACTION broadcast when the user receives an SMS or the WAP_PUSH_DELIVER_ACTION broadcast when the user receives an MMS. The default SMS app is responsible for writing details to the SMS Provider when it receives or sends a new message.

Other apps that are not selected as the default SMS app can only read the SMS Provider...

You can see More info here 刚才提到了下面的重要部分:

if your app is designed to behave as the default SMS app, then while your app is not selected as the default, it's important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc.

关于android - 如何删除 Android 5.0 版 lollipop 或 Kitkat 中的特定收件箱消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579293/

相关文章:

android - 模拟返回 Kotlin Coroutines Deferred 类型的方法的返回值

java - 如何以编程方式从 android 收件箱中读取未读短信?

android - 在 BroadcastReceiver 中,对于 SMS 发送和传递操作,getResultCode() 始终返回 -1

iphone - iOS 6.0 上的 MFMessageComposeController 中 UIButton 为 nil

android - 宽度 :100% in CSS not rendering well in Android 4. 4

java - 首先调用构造函数或 onDraw()

Android TextView Drawables 数组的开始、顶部、结束和底部常量

android - 用户完成编辑后如何从 EditText 中移除焦点?

android - 单击项目时取消选中项目

Android 4.1 至 4.4 KitKat - 为 API 启用 TLS 1.2