如何在Android 6中请求用户权限 [英] How to ask for user permissions in Android 6

查看:86
本文介绍了如何在Android 6中请求用户权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Android 6(棉花糖)中的新权限系统,我完全感到困惑.

I am totally confused regarding the new permission system in Android 6 (Marshmallow).

我正在使用两个所谓的"dangeorus权限"-

I am using two so called 'dangeorus permissions' -

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

而且我在许多地方都使用了这些权限.

And I use these permissions in many places.

我需要访问用于注册应用程序,发送反馈等的用户帐户,还需要访问设备ID来注册应用程序.

I need to access the user accounts for Registering the app, Sending Feedback etc.And I also access Device Id for registering the app.

我编写了两个函数来获取名为util的Java类中的邮件帐户列表和设备ID,并在AsyncTask和片段中使用这些函数.

I wrote two functions to get the list of Mail Accounts and the device id in a Java Class named util, and uses these function in AsyncTask as well as fragments.

现在,我完全不知所措,如何请求这些权限!我应该询问功能还是每次调用这些功能?在AsyncTask中,如何获得响应?

Now, I am totally at loss, how to request for those permissions! Should I ask in the functions or every time I call these functions? And in the AsyncTask, how can I get a response?

请帮助.

推荐答案

您不应在AsyncTask中请求权限.实际上,您甚至都不应该检索deviceId或其中的某些信息-将其作为参数传递.

You shouldn't ask for permissions in AsyncTask. In fact you shouldn't even retrieve deviceId or some information in it - pass it as a parameter.

每个应用程序的设置都有危险"权限列表及其状态.如果您将应用程序定位为sdk 23,则默认情况下将其全部禁用.因此,当您使用需要此类许可的代码时,需要检查是否已授予该代码,是否需要-要求它.

Settings of each application has list of 'dangerous' permissions with their status. If you app targeting sdk 23, they're all disabled by default. So, when you use code which requires such permission, you need to check if it's granted and if not - ask for it.

最简单的方法是为这样的权限创建实用程序类:

The simplies way is to create utility class for permissions like this:

public final class PermissionUtils {

    private PermissionUtils() {
    }

    public static boolean checkPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (!checkPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    public static boolean checkPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean isDeviceInfoGranted(Context context) {
        return checkPermission(context, Manifest.permission.READ_PHONE_STATE);
    }

    public static void requestPermissions(Object o, int permissionId, String... permissions) {
        if (o instanceof Fragment) {
            FragmentCompat.requestPermissions((Fragment) o, permissions, permissionId);
        } else if (o instanceof Activity) {
            ActivityCompat.requestPermissions((AppCompatActivity) o, permissions, permissionId);
        }
    }
}

然后像这样使用它: (在您的片段/活动中)

And then use it like this: (in your Fragment / Activity)

if (PermissionUtils.isDeviceInfoGranted(this)) {
    //get deviceId and proccess your code
} else {
    String[] permissions = new String[]{READ_PHONE_STATE};
    PermissionUtils.requestPermissions(this, PHONE_STATE_PERMISSION_ID, permissions);
}

在您的AppCompatActivity中覆盖权限结果处理程序或实现OnRequestPermissionsResultCallback接口:

Override handler for permission result in your AppCompatActivity or implement OnRequestPermissionsResultCallback interface:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PHONE_STATE_PERMISSION_ID:
            boolean granted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
            if (granted) {
                //get deviceId and proccess your code
            } else {
                //nobody knows what to do
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

如您所见,我们仍然有一个问题-如果用户拒绝许可该怎么办?作为变体,我们可以显示说明对话框.或者(可能更好)是仅在入网屏幕后请求权限.

As you see, we still have a problem - what to do, if user denied permission? As a variant, we can show explaining dialog. Or (may be better) ask for permissions only after onboarding screens.

这篇关于如何在Android 6中请求用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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