How to deal with removal of a permission for a broadcast receiver in Android M? -
i've got legacy code making permission safe marshmallow.
there broadcast using phone_state permission 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>
if phone_state permission granted later user denied when there phone call there permissions related crash. crash occurs before broadcast receiver's onreceive() called (the crash in android.app.activitythread.handlereceiver). means broadcast receiver not chance check if permission granted or not , deal situation.
so question is, if there broadcast receiver such how can code deal situation user has disabled permission because afaik there no api monitor changes in permissions occur, therefore code cannot know on fly permission has been revoked, , therefore can't deregister broadcast receiver.
as permission in android marsmallow may want check permission before receiver called this:
// here, thisactivity current activity if (contextcompat.checkselfpermission(thisactivity, manifest.permission.phone_state) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(thisactivity, manifest.permission.phone_state)) { // show expanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. } else { // no explanation needed, can request permission. activitycompat.requestpermissions(thisactivity, new string[]{manifest.permission.phone_state}, my_permissions_request_phone_state); // my_permissions_request_read_contacts // app-defined int constant. callback method gets // result of request. } }
it late answer hope helps someone!!!
Comments
Post a Comment