无法以编程方式删除 SMS(默认 SMS 应用程序集)

Posted

技术标签:

【中文标题】无法以编程方式删除 SMS(默认 SMS 应用程序集)【英文标题】:Cannot delete SMS programatically (Default SMS app set) 【发布时间】:2016-11-30 03:10:38 【问题描述】:

我尝试使用此功能删除我手机中的所有短信

 public void wipeOutAllSMS(Context context) 
    try 

        int rowsDeleted = 0;

        Uri uriSms = Uri.parse("content://sms/inbox");
        Cursor c = context.getContentResolver().query(uriSms, new String[]"_id", "thread_id", "address", "person", "date", "body", "read=0", 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);
                String date = c.getString(3);

                // mLogger.logInfo("Deleting SMS with id: " + threadId);
                rowsDeleted += context.getContentResolver().delete(Uri.parse("content://sms/" + id), "date=?", new String[]c.getString(4));

                Log.i("Deleting_SMS", "Delete success for: " + address + ", message = " + body);

             while (c.moveToNext());
        

        Log.i("Deleting_SMS", "rows deleted: " + rowsDeleted);

     catch (Exception e) 
        Log.e("Deleting_SMS", e.toString());
    

不幸的是,“rows_deleted”始终为 0。它不会删除一条短信。

我已在清单中添加了以下内容:

权限:

  <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />

为了能够选择我的应用作为默认短信阅读器的其他事项:

<!--The following things are needed for this app to became the default sms app in order to have the ability to delete an sms. -->

        <!-- BroadcastReceiver that listens for incoming SMS messages -->
        <receiver
            android:name=".android.broadcast.SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>


        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver
            android:name=".android.broadcast.MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- This one needs too-->


        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".android.broadcast.HeadlessSmsSendService"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>

我还请求启动应用程序的权限,我最终接受了:

if (!PermissionHelper.isPermissionGranted(this, Manifest.permission.SEND_SMS) || !PermissionHelper.isPermissionGranted(this, Manifest.permission.READ_SMS)) 
            PermissionHelper.requestPermission(this,
                    new String[]
                    Manifest.permission.SEND_SMS,
                    Manifest.permission.READ_SMS
            );

WRITE_SMS 权限在 Manifest.permisson 中不存在,所以我不能要求。)

我还将我设备上的默认短信阅读器设置为我的应用程序。但是我无法删除一条短信。

如果可以的话,请帮忙。

我用的是安卓Marshmallow,没有抛出异常。

【问题讨论】:

你有没有在logcat中看到任何消息 【参考方案1】:

如果你是默认应用,那么你可以像这样删除。

  Uri.Builder builder;
  builder = Sms.CONTENT_URI.buildUpon();
  //builder.appendEncodedPath(getSms_id());
  builder.appendEncodedPath("your SMS ID here");
  Uri uri = builder.build();
  context.getContentResolver().delete(uri, null, null);

【讨论】:

以上是关于无法以编程方式删除 SMS(默认 SMS 应用程序集)的主要内容,如果未能解决你的问题,请参考以下文章

SMS Retriever API - 如何以编程方式获取 SMS?

如何在android中以编程方式从收件箱中删除所有短信?

如何在MIUI 8+中获取读取“服务SMS”的权限(以编程方式)

无法将我自己的 SMS 应用程序设置为默认值。任何建议如何解决它? [复制]

如何在 Android Studio 中创建项目以运行默认 SMS 应用程序

Android RoleManager 为自己的包删除角色 RoleManager.ROLE_SMS