我有一个BaseAdapter,其中包含所有需要权限的列表才能正确使用该应用程序。
我需要在onClick中的一个之后请求权限并检查是否被授予。
对于请求,我正在使用
ActivityCompat.requestPermissions
但我不知道如何得到结果。
是否可以调用O
nRequestpermissionsResultCallback
知道用户的答案?
我该怎么做?
答案 0 :(得分:0)
要检查权限(在此示例中,它是使用相机的权限):
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
// permission is not granted yet, let's ask for it
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MyActivity.cameraRequestCode);
} else {
// permission is already granted
}
如果运行了ActivityCompat.requestPermissions,则会为用户显示一个对话框,之后您可以通过覆盖Activity的onRequestPermissionsResult来捕获结果:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MyActivity.cameraRequestCode: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// action when permission granted
} else {
// action when permission denied
}
return;
}
}
}
MyActivity.cameraRequestCode是一个静态int,可以是任何唯一的int值。
答案 1 :(得分:0)
在您的适配器类
中class Adapter extends RecyclerView.Adapter<Your adapter.MyViewHolder> implements ActivityCompat.OnRequestPermissionsResultCallback{`//enter code here`}
它将覆盖onRequestPermissionsResult
答案 2 :(得分:0)
我在RecylclerView.Adapter中遇到相同的问题,并且在权限请求方法中使用了本节
private void requestCallPhonePerm() {
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
onItemCallListener.itemCallPhone(itemPhones);
ActivityCompat.requestPermissions((Activity)context,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else {
((MainActivity)context).callPhone(itemPhones);
}
}
在“ requestPermissions”方法中使用(Activity)context 来在适配器的容器活动中调用回调很重要
在包含回收者的MainActivity中,我通常使用覆盖的请求权限结果。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
if(grantResults.length>0
&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
new LoadContacts().execute();
}
break;
case MY_PERMISSIONS_REQUEST_CALL_PHONE:
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
callPhone(phones);
}else{
}
}
}
答案 3 :(得分:0)
我从定制类解决了这个问题。我的自定义类源自AppCompatActivity
。另外,您还需要将Activity上下文传递给自定义类,并对其进行所需的操作。
public bool AreStorageAndCamPermissionsGranted(Activity context)
{
if (Build.VERSION.SdkInt >= Build.VERSION_CODES.M)
if (
context.CheckSelfPermission(Manifest.Permission.Camera)!= Android.Content.PM.Permission.Granted
|| context.CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Android.Content.PM.Permission.Granted
|| context.CheckSelfPermission(Manifest.Permission.WriteExternalStorage) != Android.Content.PM.Permission.Granted)
{
ActivityCompat.RequestPermissions(context, new String[]
{
Manifest.Permission.Camera,
Manifest.Permission.ReadExternalStorage,
Manifest.Permission.WriteExternalStorage,
}, REQUEST_PERMISSION_CODE);
}
return true;
}
`
答案 4 :(得分:0)
所以首先我在适配器中实现 实施ActivityCompat.OnRequestPermissionsResultCallback 那么需要一种Overide方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// it is not work here onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == storagePermission) {
// getPosts(false);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context.getApplicationContext(), "Please allow permission to Continue", Toast.LENGTH_SHORT).show();
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context.getApplicationContext(), " Permissions Granted Succecfully", Toast.LENGTH_SHORT).show();
//here i load my method that i want to run
}
}
}
现在的问题是这个 onRequestPermissionsResult 不是从适配器调用的 mainActivity与适配器连接的适配器触发 onRequestPermissionsResult
因此,在主要活动中,我创建了这个并通过传递来运行适配器的 onRequestPermissionsResult adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == storagePermission) {
// getPosts(false);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
//permission not granted
}
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted
adapterPosts.onRequestPermissionsResult(requestCode,permissions,grantResults);
//loadImageIntent();
}
}
}