如何从Android上的应用程序中删除手机收件箱的短信

时间:2016-11-22 05:57:53

标签: android

我收到错误:

Permission Denial:writing`com.android.provider.telephony.smsprovider uri content://sms/472 from pid=11211, uid=10142 requires
android.permission.write_sms, or granturipermission()

请帮我解决这个问题,因为我是新手。

我的代码是:

public void deleteSMS()
{
    Uri inboxUri = Uri.parse("content://sms/inbox");
    int count = 0;
    Cursor c = getContentResolver().query(inboxUri , null, null, null, null);
    while (c.moveToNext()) {
        try {
            // Delete the SMS
            String pid = c.getString(0); // Get id;
            String uri = "content://sms/" + pid;
            count =getContentResolver().delete(Uri.parse(uri),
                    null, null);
            Toast.makeText(getApplicationContext(),"message deleted successfully..!"+count, 5000).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(), 5000).show();
        }
    }}

Added Permission in manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE"/>
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

我添加了所有权限,但我仍然没有得到答案,并显示相同的错误...

3 个答案:

答案 0 :(得分:2)

正如您所说,您在4.2.2上运行代码,不需要请求权限。但是要删除SQL,您必须在ODI 11g中添加这些权限:

SMS

您错过了AndroidManifest.xml权限,在删除操作之前,您必须执行需要它的读取操作。

答案 1 :(得分:0)

您是否已获准阅读清单中的短信?如果您在Android 6.0及更高版本上进行测试,则必须在运行时提供权限。

阅读联系人的示例:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) !=
    PackageManager.PERMISSION_GRANTED) {

    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {


    }
    else {
        ActivityCompat.requestPermissions(
            thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }
}

答案 2 :(得分:0)

在Android 6.0(Marshmallow)及以上版本中,您必须在运行时获取权限。

 requestPermissions(new String[]{Manifest.permission.READ_SMS}, 1).
  

检查您是否获得了许可。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (ActivityCompat.checkSelfPermission(
                      getActivity(), Manifest.permission.READ_SMS) ==
                    PackageManager.PERMISSION_GRANTED)
                {