如何处理Android M中广播接收者权限的删除? [英] How to deal with removal of a permission for a broadcast receiver in Android M?

查看:74
本文介绍了如何处理Android M中广播接收者权限的删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些遗留代码,正在使棉花糖的权限安全.

I've got some legacy code which I am making permission safe for Marshmallow.

使用PHONE_STATE权限进行广播,如下所示:

There is a broadcast using the PHONE_STATE permission as follows:

<receiver android:name="redacted.TheBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"></action>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

如果授予PHONE_STATE权限,但随后用户拒绝,则当有电话呼叫时,将发生与权限相关的崩溃. 但是崩溃发生在调用广播接收器的onReceive()之前(崩溃在android.app.ActivityThread.handleReceiver中).这意味着广播接收器甚至没有机会检查是否授予了许可并处理这种情况.

If the PHONE_STATE permission is granted but then later the user denied then when there is a phone call there is a permissions related crash. But the crash occurs before the Broadcast Receiver's onReceive() is called (the crash is in android.app.ActivityThread.handleReceiver). That means that the broadcast receiver does not get the chance to even check if the permission is granted or not and deal with that situation.

所以我的问题是,如果有这样的广播接收器,那么该代码如何处理由于AFAIK用户禁用权限而导致用户禁用权限的情况,因此没有API可以监视权限发生的更改,因此代码无法即时得知该权限已被撤消,因此它无法注销其广播接收器.

So my question is, if there is a broadcast receiver such as this how can the code deal with the situation where the user has disabled the permission because AFAIK there is no API to monitor for changes in permissions as they occur, therefore the code cannot know on the fly that the permission has been revoked, and therefore it can't deregister its broadcast receiver.

推荐答案

至于Android Marsmallow中的权限,您可能需要在这样调用接收方之前检查权限:

As for the permission in Android Marsmallow you may want to check for permission before your receiver is called like this:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.PHONE_STATE)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.PHONE_STATE)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.PHONE_STATE},
                MY_PERMISSIONS_REQUEST_PHONE_STATE);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

这是一个很晚的答案,但我希望它能对某人有所帮助!

It is a late answer but i hope it helps someone!!!

这篇关于如何处理Android M中广播接收者权限的删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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