gpt4 book ai didi

android - 在棉花糖中授予运行时权限后如何通知recyclerview适配器

转载 作者:行者123 更新时间:2023-12-03 21:35:35 26 4
gpt4 key购买 nike

我有一个 Activity ,我正在加载一个 fragment 。我正在使用 recyclerviewadapter 在 recyclerview 中加载此 fragment 中的数据。

在 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() 方法返回任何 bool 变量,如果授予或不授予权限,它将返回 true/false,以便在适配器中我可以检查这个变量并可以继续我的任务。

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

非常感谢先进。

最佳答案

你的问题不够清楚。但是,如果您要实现的是授予运行时权限 OnClick 适配器类中的按钮,请尝试以下操作:
在您的适配器类中,创建一个接口(interface),例如

public interface WillReceiveClickFromFragment {
void receiveClick();
}
现在,仍然在您的适配器类中创建接口(interface)类型的变量“WillReceiveClickFromFragment”并将其传递给您的适配器类构造函数
private WillReceiveClickFromFragment willReceiveClickFromFragment;

public YourAdapterClassConstructor(..., WillReceiveClickFromFragment willReceiveClickFromFragment){
this.willReceiveClickFromFragment = willReceiveClickFromFragment;
}
最后,在您执行单击操作的适配器类内的按钮中,将其放入您的 OnClickListener
yourButton.setOnClickListener(v -> {
WillReceiveClickFromFragment.receiveClick();
});
现在在你的 Fragment 类中,实现你刚刚在适配器类中创建的接口(interface)并重写接口(interface)方法
public class YourFragmentClassName extends Fragment implements YourAdapterClassName.WillReceiveClickFromFragment {

...

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

关于android - 在棉花糖中授予运行时权限后如何通知recyclerview适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35916892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com