onRequestPermissionsResult

onRequestPermissionsResult

本文介绍了不从片段调用onRequestPermissionsResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android项目中,我使用此代码检查&请求运行时权限:

In my android project, I use this code to check & request runtime permissions:

public class AgentsFragmentMapTab extends RootFragment implements  ActivityCompat.OnRequestPermissionsResultCallback 
{    
     //..
     private boolean checkAndRequestPermissions() {
            int internet = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.INTERNET);
            int loc = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
            int loc2 = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
            List<String> listPermissionsNeeded = new ArrayList<>();
            if (internet != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.INTERNET);
            }
            if (loc != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }
            if (loc2 != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (BuildConfig.DEBUG)
                Log.d(TAG, "checkAndRequestPermissions_listPermissionsNeeded(" + listPermissionsNeeded.size() + ")");
            if (!listPermissionsNeeded.isEmpty()) {
                ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), LOCATION_REQ_CODE);
                return false;
            }
            return true;
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

            if (requestCode == LOCATION_REQ_CODE) {

                } else {

                }
            }
        }

此代码完美显示了权限对话框.但是,当我单击允许拒绝按钮时,方法onRequestPermissionsResult()不会被调用.为什么不调用它以及如何实现呢?

This code shows the permission dialog perfectly. But when I click on the Allow or Deny button, the method onRequestPermissionsResult() doesn't get invoked. Why isn't it getting invoked and how to achieve this?

推荐答案

如注释中所述覆盖父活动中的onRequestPermissionsResult

As mentioned in comments Override onRequestPermissionsResult in the parent activity

确保从此活动的onRequestPermissionsResult

然后它将发送到覆盖onRequestPermissionsResult

这就是我所做的

我叫requestPermissions(StringOfPermissions,YourRequestCode);

在片段内

这是我用来在活动内添加片段的代码,很简单

And Here is the code i used to add the fragment inside the activity , pretty straight forward

getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.container, BlankFragment.newInstance("",""))
            .disallowAddToBackStack()
            .commit();

我覆盖了onRequestPermissionsResult():

I overridden onRequestPermissionsResult():

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

活动和片段内

当我从片段中请求时,它进入活动,然后进入片段

when i requested from the fragment it entered the activity and then it entered the fragment

这篇关于不从片段调用onRequestPermissionsResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:58