0

我有一个对话框,它加载带有 5 个部分的 tabLayout,它的内容是使用带有 View Pager 适配器的 ViewPager 加载的。现在,在其中一个部分中,我必须从图库中加载图像。我已经编写了运行时权限,但是我无法使用权​​限的回调,因为我在没有活动的类中实现了这个对话框。我不知道如何在无活动的类中实现权限的回调方法。

public void makePermissionRequest(){ if(ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(showEditorActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, permission_request_code); }else{ fetchImagesFromDevice(); } }

和回调..

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case permission_request_code: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i("permission..link", "allowed");
                fetchImagesFromDevice();

            }else{
                Log.i("permission..link", "denied");
                if (ContextCompat.checkSelfPermission(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(showEditorActivity, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                        showMessageOKCancel("You need to allow access to your Internal Memory", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //log to find out answer..
                                if (which == -1) {
                                    makePermissionRequest();
                                }
                            }
                        });
                    }

                }
            }

        }

    }
}
4

0 回答 0