为什么Android会忽略READ_SMS权限? [英] Why does Android ignore READ_SMS permission?

查看:1107
本文介绍了为什么Android会忽略READ_SMS权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Android API 15下的阅读收件箱,我遇到了以下问题:

I am playing with reading inbox under Android API 15 and I am stuck on the following problem:

我的应用只有一个活动,默认启动主要活动。它有 onCreate 代码

My app has just one activity, main one launched by default. It has this onCreate code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unlock);


        // Create Inbox box URI
        Uri inboxURI = Uri.parse("content://sms/inbox");

        // List required columns
        String[] reqCols = new String[] { "_id", "address", "body" };

        // Get Content Resolver object, which will deal with Content Provider
        ContentResolver cr = getContentResolver();

        // Fetch Inbox SMS Message from Built-in Content Provider
        Cursor c = cr.query(inboxURI, reqCols, null, null, null);

    }

现在,虽然此代码没有任何用处,但只需提取数据并准备游标,以便我可以遍历它们,它会导致以下错误:

Now while this code does nothing useful, just fetches the data and prepares cursor so that I can iterate through them, it causes the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cryptail.stealthsms/com.cryptail.stealthsms.UnlockActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=4362, uid=10059 requires android.permission.READ_SMS, or grantUriPermission()

该行出现错误 Cursor c = cr.query 代码,并敦促我使用READ_SMS权限。

The error occures on the line with Cursor c = cr.query code, and urges me to use READ_SMS permission.

这是我的清单XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cryptail.stealthsms" >


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name=".UnlockActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:naame="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

您可以看到包含的权限。造成这种情况的原因是什么?

You can see the permission is included. What may be causing this?

编辑28.9.2015 - 我没有说明我在Android工作室使用Android Emulator,具体是Android 6.0 (API 23)。在具有不同Android版本(4.4.2)的另一个模拟设备下,此代码可用。那么可能是Android 6.0或模拟器本身的错误? A6.0中是否有关于SMS权限的任何更改?

EDIT 28.9.2015 - I did not specify I was working with Android Emulator in Android studio, concretely Android 6.0 (API 23). Under another emulated devices with different Android version (4.4.2) this code works. So maybe a bug in Android 6.0 or in the emulator itself? Are there any changes in A6.0 regarding SMS permissions?

推荐答案

所以问题是TDG提到的新权限模型在Android M中。

So the problem is, as mentioned by TDG, new permission model in Android M.

本文帮助我以比官方Android文档

只需使用

 if(ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {

在执行任何SMS权限相关代码之前,如果该权限不存在,请使用

before any SMS permission related code is executed, and if the permission is not present, use

final int REQUEST_CODE_ASK_PERMISSIONS = 123;
ActivityCompat.requestPermissions(UnlockActivity.this, new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);

这篇关于为什么Android会忽略READ_SMS权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆