棉花糖新许可模型

时间:2016-03-10 11:07:22

标签: android android-6.0-marshmallow

我希望通过向我的应用提供权限模型来在应用程序中实现新的marshmallow支持。 现在我在清单文件中给出了以下权限。

  

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

现在,在一个活动中,我加载了一个片段,其中我有一个recyclerview,并使用recyclerview适配器在此recyclerview中加载数据。

在这个rec​​yclerview适配器列表中,我点击了一个按钮,我需要上述权限。所以,当用户点击这个按钮时,我调用了以下功能。

    public void checkForManifestPermission(){

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

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {
            // 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.

            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);

        } else {

            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions((Activity)mContext,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    ConstantVariables.READ_EXTERNAL_STORAGE);

            // 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 ConstantVariables.READ_EXTERNAL_STORAGE: {
            // 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.
                ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;

            } else {

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

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

在我调用函数 checkForManifestPermission 的适配器中并检查条件 if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){} 然后只重定向到其他活动。

现在我的问题是在调用onRequestPermissionsResult方法之前执行此条件中编写的代码,我希望如果用户授予权限,那么只有它应该重定向到其他活动。

我如何实现这一点,我希望我的代码等待用户回复批准或拒绝许可,然后再进一步。

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:0)

试试这个:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == PERMISSIONS_CODE) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    onPPSButtonPress();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_CODE);
                }
            }
        }
    }
}

注意:如上所述添加您的阅读权限代码。

答案 1 :(得分:0)

您可以在marshmallow中设置以下代码以获取检查权限:

if ((int) Build.VERSION.SDK_INT >= 23) {
                if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider WRITE_EXTERNAL_STORAGE
                    if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    } else {
                        ActivityCompat.requestPermissions(mActivity,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
                    }
                    return;
                }
            }

答案 2 :(得分:0)

我在两个不同的项目中遇到过类似的问题。

在其中一个我只是检查了进入活动的权限。 如果没有给出许可,那么我禁用了按钮。

如果这不实用,那么另一种可能性就是采用正如你所描述的那样的代码,&#34;在你的情况下&#34;并把它放到一个单独的方法。 然后更改checkForManifestPermission()以返回一个关于程序是否已具有所需权限的布尔值。

所以,代码的那部分会读到这样的内容:

private void methodThatNeedsPermission(){
   if (checkForManifestPermission())
       doTheAction();
}

 private void doTheAction(){
  ---- your code
}

现在,在onRequestPermissionsResult中,如果用户获得了权限,则可以调用相同的方法。所以,它变成了:

    @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE: {
            // 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.
            ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true;
            doTheAction(); 

        } else {

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

    // other 'case' lines to check for other
    // permissions this app might request
}
相关问题