Java Android M 权限:未调用 onRequestPermissionsResult()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32714787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:58:58  来源:igfitidea点击:

Android M Permissions: onRequestPermissionsResult() not being called

javaandroidpermissionsandroid-permissions

提问by AndyOHart

I'm updating our app to use the new M runtime permissions system. It's all working except for onRequestPermissionsResult(). I need to check a permission on a button press, and if it's successful, send a text message. When I grant permission to do it, the dialog closes, but it doesn't trigger the Send Text until I press the button again.

我正在更新我们的应用程序以使用新的 M 运行时权限系统。除了 onRequestPermissionsResult() 之外,一切正常。我需要检查按下按钮的权限,如果成功,请发送短信。当我授予这样做的权限时,对话框关闭,但在我再次按下按钮之前它不会触发发送文本。

I've debugged and set breakpoints in the onRequestPermissionsResult() method but it never goes into it.

我已经在 onRequestPermissionsResult() 方法中调试并设置了断点,但它从未进入它。

This method gets called first:

这个方法首先被调用:

    private void askForPermission() {
    String[] permissions = new String[]{Manifest.permission.SEND_SMS};
    ActivityCompat.requestPermissions(getActivity(), permissions, PERMISSIONS_CODE);
}

And then my callback looks like this:

然后我的回调看起来像这样:

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

    if (requestCode == PERMISSIONS_CODE) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.SEND_SMS)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    onPPSButtonPress();
                } else {
                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSIONS_CODE);
                }
            }
        }
    }
}

Has anybody run into a similar issue? Appreciate any help with this. Thanks

有没有人遇到过类似的问题?感谢您对此的任何帮助。谢谢

采纳答案by AndyOHart

This issue was actually being caused by NestedFragments. Basically most fragments we have extend a HostedFragment which in turn extends a CompatFragment. Having these nested fragments caused issues which eventually were solved by another developer on the project.

这个问题实际上是由 NestedFragments 引起的。基本上,我们扩展了一个 HostedFragment 的大多数片段,而后者又扩展了一个 CompatFragment。拥有这些嵌套片段导致的问题最终由项目的另一位开发人员解决。

He was doing some low level stuff like bit switching to get this working so I'm not too sure of the actual final solution

他正在做一些低级的东西,比如位切换来让它工作,所以我不太确定实际的最终解决方案

回答by Tieru

You can try this:

你可以试试这个:

requestPermissions(permissions, PERMISSIONS_CODE);

If you are calling this code from a fragment it has it's own requestPermissions method. I believe the problem is that you are calling static method.

如果您从片段中调用此代码,则它具有自己的 requestPermissions 方法。我相信问题在于您正在调用静态方法。

Pro Tip if you want the onRequestPermissionsResult()in a fragment: FragmentCompat.requestPermissions(Fragment fragment, String[] permissions, int requestCode)

专业提示,如果您想要onRequestPermissionsResult()片段: FragmentCompat.requestPermissions(Fragment fragment, String[] permissions, int requestCode)

回答by Marta Rodriguez

You have the checkPermissionsfunction for pre Marshmallow devices in FragmentCompat. I use like this:

您在FragmentCompat 中具有用于 Marshmallow 前设备的checkPermissions功能。我这样使用:

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

回答by yavor87

I ran into the same issue and I just found the solution. When using the Support library, you have to use the correct method calls. For example:

我遇到了同样的问题,我刚刚找到了解决方案。使用 Support 库时,您必须使用正确的方法调用。例如:

  • When in AppCompatActivity, you should use ActivityCompat.requestPermissions;
  • When in android.support.v4.app.Fragment, you should use simply requestPermissions(this is an instance method of android.support.v4.app.Fragment)
  • AppCompatActivity 中时,您应该使用ActivityCompat.requestPermissions
  • android.support.v4.app.Fragment 中时,您应该简单地使用requestPermissions(这是 android.support.v4.app.Fragment 的实例方法)

If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResultcallback is called on the activity and not the fragment.

如果您在片段中调用 ActivityCompat.requestPermissions,则会在活动而非片段上调用onRequestPermissionsResult回调。

Hope this helps!

希望这可以帮助!

回答by Dusan Zivkovic

If you have onRequestPermissionsResultin both activity and fragment, make sure to call super.onRequestPermissionsResultin activity. It is not required in fragment, but it is in activity.

如果您同时拥有onRequestPermissionsResult活动和片段,请确保调用super.onRequestPermissionsResult活动。片段中不需要它,但它在活动中。

回答by AkKi

If you are using requestPermissions in fragment, it accepts 2 parameters instead of 3.

如果您在片段中使用 requestPermissions,它接受 2 个参数而不是 3 个。

You should use requestPermissions(permissions, PERMISSIONS_CODE);

你应该使用 requestPermissions(permissions, PERMISSIONS_CODE);

回答by TouchBoarder

If for some reason you have extended a custom Activity located in some external library that do not call the super you will need to manually call the Fragment super.onRequestPermissionsResult yourself in your Activity onRequestPermissionsResult.

如果出于某种原因,您扩展了位于某些不调用 super 的外部库中的自定义活动,您将需要在您的活动 onRequestPermissionsResult 中手动调用 Fragment super.onRequestPermissionsResult。

YourActivity extends SomeActivityNotCallingSuperOnRequestPermissionsResult{
Fragment requestingFragment;//the fragment requesting the permission
...
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestingFragment!=null)
            requestingFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
...

回答by Sumit khare

This will work..

这将工作..

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

回答by almisoft

I have found out that is important where you call android.support.v4.app.Fragment.requestPermissions.

我发现你打电话的地方很重要android.support.v4.app.Fragment.requestPermissions

If you do it in onCreate(), onRequestPermissionsResult()is never called.

如果您在onCreate(), 中执行此操作,onRequestPermissionsResult()则永远不会调用。

Solution: Call it in onActivityCreated();

解决方案:调用它onActivityCreated()

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_DENIED) 
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 0);

}

回答by Shahab Rauf

 /* use the object of your fragment to call the 
 * onRequestPermissionsResult in fragment after
 * in activity and use different request Code for 
 * both Activity and Fragment */

   if (isFragment)
    mFragment.requestPermissions(permissions.toArray(new
    String[permissions.size()]),requestPermission);

   else
    ActivityCompat.requestPermissions(mActivity,permissions.toArray(new
    String[permissions.size()]),requestPermission);