How to deal with removal of a permission for a broadcast receiver in Android M?

前端 未结 3 558
南笙
南笙 2021-01-31 16:59

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

There is a broadcast using the PHONE_STATE permission as follows:



        
3条回答
  •  广开言路
    2021-01-31 17:57

    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!!!

提交回复
热议问题