检测权限是否被撤销

时间:2016-03-13 16:22:55

标签: android permissions m

我正在使用已植根的M设备并尝试访问其他应用的权限设置。 我想知道用户为每个应用授予或撤消了哪些权限 这可以吗?

checkSelfPermission()可以为此工作吗? 此外,如果我下载的应用程序不是针对M版本开发的,那么检测操作是否有效,因为我发现任何低于23的应用程序目标版本将始终返回PERMISSION_GRANTED

checkSelfPermission returning PERMISSION_GRANTED for revoked permission with targetSdkVersion <= 22

1 个答案:

答案 0 :(得分:0)

嗨这里是安装Android M的几个步骤,并记住你也应该在清单文件中声明相同的权限。

第1步。 声明全局变量:

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 0;

第2步。 在主要活动中使用此代码

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

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

            // 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(activity,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);

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

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

第3步。  在你的oncreate方法中调用这个方法,

locationpermission();

你可以从这里打电话给任何人,你可以用覆盖方法 onRequestPermissionsResult 来获得每一个结果。

thankyou

希望这对你有帮助(Y)。