在运行时要求权限,Android M + [英] Asking for permissions at run-time, Android M+

查看:88
本文介绍了在运行时要求权限,Android M +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道这是一个重复的问题,但是我已经解决了其他类似问题的答案,但是在任何这些解决方案中都找不到成功的机会.

Firstly, I know this is a duplicate question, but I've been over the answers from other similar questions and haven't been able to find success with any of those solutions.

我已经开发了一款可以在我的测试设备上完美运行的应用程序,它是运行Android L(5.0.1)的Samsung S4,但是我希望该应用程序也可以在较新版本的Android上运行.

I've developed an app which works perfectly on my testing device which is a Samsung S4 running Android L (5.0.1) however I would like for this app to also work on newer versions of Android.

我了解到Android M +的请求权限已更改,因此必须在运行时进行询问,但是当我尝试实现此请求时,该对话框将永远不会出现,因此也就不会要求提供所需的权限.

I understand that requesting permissions have changed for Android M+ so that they have to be asked at run-time, but when I try to implement this the dialog box never appears and hence the required permissions are never asked for.

我请求的权限是ACCESS_FINE_LOCATION.

这是清单文件中的相关代码:

Here is the relevant code from my manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

如果尚未授予许可,我会在哪里请求许可:

Where I ask for permission if it is not already granted:

if (ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

    startGeofenceMonitoring();

} else {
    askForPermission(android.Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
}

askForPermission函数(从Google的示例中稍作修改):

askForPermission function (slightly modified from Google's example):

private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(MapsActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this, permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);

        } else {

            Log.d(TAG, "askForPermission: " + permission);

            ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);
        }
    } else {
        Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
    }
}

和onRequestPermissionsResult函数:

And the onRequestPermissionsResult function:

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

    Log.d(TAG, "onRequestPermissionsResult: " + requestCode);

        switch (requestCode) {
            case 1:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Log.d(TAG, "onRequestPermissionsResult: request");

                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();

                    startGeofenceMonitoring();
                }
                break;
        }
}

正如我所说,要求权限的对话框从未出现,但我的控制台告诉我ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode);已被调用,经过大量研究后,我仍然不确定该为什么不起作用.我认为这可能是由于该活动是显示Google Maps地图的Fragment活动.我确实尝试过在另一个活动中请求权限,但是发生了完全相同的事情.

As I said, the dialog box which is asking for the permissions never appears, but my console tells me that ActivityCompat.requestPermissions(MapsActivity.this, new String[]{permission}, requestCode); is called, and after a lot of research I'm still not really sure why it isn't working. The only thing that I think it could be due to is that this Activity is a Fragment activity displaying a Google Maps map. I did try to request the permissions in another activity but the exact same thing happens.

谢谢.

编辑

因此,我刚刚在一个全新的项目中测试了我在此处发布的代码,它运行得很好,并显示了对话框.现在,我很困惑是什么原因阻止了此对话框显示在我当前的项目中.

So I just tested the code I posted here in a completely fresh project and it ran just fine and showed the dialog box. I am now really confused as to what is preventing this dialog box from being shown in my current project.

推荐答案

final private int REQUEST_CODE_ASK_PERMISSIONS = 123;


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


private void requestPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
        ActivityCompat
                .requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

这篇关于在运行时要求权限,Android M +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆