如何在marshmallow中运行时权限授予后通知recyclerview适配器

时间:2016-03-10 12:45:18

标签: android android-permissions android-6.0-marshmallow

我有一个活动,我正在加载一个片段。我正在使用recyclerviewadapter在recyclelerview中加载此片段中的数据。

在Recyclerview适配器中,点击某些按钮我需要以下权限:

READ_EXTERNAL_STORAGE

为此我已请求使用以下代码授予权限

public void checkPermission(){

    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.

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

        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

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

现在如何通知我的适配器授予了权限,现在我可以继续我的任务。

我可以从checkPermission()方法返回任何布尔变量,如果授予了权限,它将返回true / false,这样在适配器中我可以检查这个变量并继续我的任务。

如果有人在这里有任何想法,请帮助我。

非常感谢先进。

1 个答案:

答案 0 :(得分:0)

您的问题还不够清楚。但是,如果您要实现的是授予运行时权限OnClick适配器类中的按钮,请尝试以下操作:

在您的适配器类中,创建一个接口,例如

public interface WillReceiveClickFromFragment {
    void receiveClick();
}

现在,仍然在适配器类中创建接口类型为'WillReceiveClickFromFragment'的变量,并将其传递给适配器类的构造函数

private WillReceiveClickFromFragment willReceiveClickFromFragment;

public YourAdapterClassConstructor(..., WillReceiveClickFromFragment willReceiveClickFromFragment){
    this.willReceiveClickFromFragment = willReceiveClickFromFragment;
}

最后,在执行单击操作的适配器类内的按钮中,将其放入OnClickListener

yourButton.setOnClickListener(v -> {
    WillReceiveClickFromFragment.receiveClick();
});

现在在您的Fragment类中,实现您刚刚在适配器类中创建的接口并覆盖该接口方法

public class YourFragmentClassName extends Fragment implements YourAdapterClassName.WillReceiveClickFromFragment {

                  ...

    @Override
    public void receiveClick(){
        /*call your check permisson method here*/
        checkPermission();
    }
}