我有一个片段,其中我有recyclerview和设置数据在这个recyclerview使用recyclerview适配器。

现在,我在适配器的列表项中有一个按钮,点击它,我需要检查android中的READ_EXTERNAL_STORAGE权限,以获得android中的新权限模型。

我在这个适配器的片段中创建了一个新函数,以检查是否授予权限,如果没有授予,则请求权限。

我已经通过了MyFragment。这作为适配器中的一个参数,并在适配器中的按钮单击上调用片段的方法。

我已经使用下面的代码在片段中调用requestPermission。

if(ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
       requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                ConstantVariables.READ_EXTERNAL_STORAGE);
    }

我用下面的代码重写了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, proceed to the normal flow.
                startImageUploading();
            } else {}

但是它没有被调用,而不是这个Activity的onRequestPermissionsResult方法被调用。

我在片段的父活动中也定义了相同的onRequestPermissionsResult方法,它正在被调用。

我不能删除活动的onRequestPermissionsResult方法,但想调用片段的onRequestPermissionsResult方法时,我请求从片段的权限。 我该怎么做呢?我做错了什么吗,如果有人有什么想法,请帮助我。


当前回答

如果您正在使用Kotlin,您应该确切地指定您正在调用片段方法,而不是活动

 fragment.requestPermissions(permissions, PERMISSIONS_CODE);

Android M permission: onRequestPermissionsResult()未被调用

从v4请求运行时权限。片段和回调到片段?

其他回答

在片段中,你不应该为requestPermissions使用ActivityCompat,你只需要使用requestPermissions并在方法中传递两个参数。

权限字符串 RequestCode。

对于exp:

 requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);

如果您正在使用Kotlin,您应该确切地指定您正在调用片段方法,而不是活动

 fragment.requestPermissions(permissions, PERMISSIONS_CODE);

Android M permission: onRequestPermissionsResult()未被调用

从v4请求运行时权限。片段和回调到片段?

你可以调用下面的Fragment方法

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_CODE:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission is granted. Continue the action or workflow
                // in your app.
                doActionBecauseNowYouCanBro();
            } else {
                // Explain to the user that the feature is unavailable because
                // the features requires a permission that the user has denied.
                // At the same time, respect the user's decision. Don't link to
                // system settings in an effort to convince the user to change
                // their decision.
                showWhyRequestPermissionsAndDontBlockUserItsCalledManners();
            }
            return;
    }
    // Other 'case' lines to check for other
    // permissions this app might request.

}

requestPermissions on Fragments方法要求API级别23或更高。 如果你的应用瞄准的是较低的版本,你可以使用

FragmentCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            ConstantVariables.READ_EXTERNAL_STORAGE);

首先你需要添加support-v13依赖:

implementation "com.android.support:support-v13:$supportLibVersion"

我也有同样的问题。你的片段可以从活动布局中初始化。 像这样:

main_activty.xml

<fragment
    android:id="@+id/fragment"
    android:name="com.exampe.SomeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

当我使用FragmentTransaction代替时,这个问题为我解决了