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

查看:19
本文介绍了为什么 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?

EDIT 28.9.2015 - 我没有指定我在 Android Studio 中使用 Android Emulator,具体来说是 Android 6.0 (API 23).在另一个具有不同 Android 版本 (4.4.2) 的模拟设备下,此代码有效.那么也许是 Android 6.0 或模拟器本身的错误?A6.0 有没有关于短信权限的变化?

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.

这篇文章 帮助我以比 official android doc 更清晰的方式理解了这个问题.

简单使用

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

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

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天全站免登陆