Android 6.0 中的 Android 运行时权限(API 级别 23)最佳实践 [英] Android Run time Permissions in Android 6.0 (API level 23) best practices

查看:17
本文介绍了Android 6.0 中的 Android 运行时权限(API 级别 23)最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 6.0(API 级别 23)最佳实践中的 Android 运行时权限

Android Run time Permissions in Android 6.0 (API level 23) best practices

我已经成功地使用

这些参考

http://developer.android.com/training/permissions/requesting.html

https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html

这是我用过的代码

private static final int PERMISSION_REQUEST_CODE = 1;

    switch (id){
                case R.id.check_permission:
                    if (checkPermission()) {

                        Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                    } else {

                        Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
                    }
                    break;
                case R.id.request_permission:
                    if (!checkPermission()) {

                        requestPermission();

                    } else {

                        Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

                    }
                    break;
            }

private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED){

            return true;

        } else {

            return false;

        }
    }

    private void requestPermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)){

            Toast.makeText(context,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Snackbar.make(view,"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();

                } else {

                    Snackbar.make(view,"Permission Denied, You cannot access location data.",Snackbar.LENGTH_LONG).show();

                }
                break;
        }
    }

android 提供的最佳实践

The best practices provided by android

http://developer.android.com/training/permissions/best-实践.html

陈述以下内容

1) 考虑使用 Intent

1) Consider Using an Intent

2) 只请求你需要的权限

2) Only Ask for Permissions You Need

3) 不要压倒用户

4) 解释为什么需要权限

4) Explain Why You Need Permissions

现在按照上述内容,我们仅在需要时请求权限,并且仅针对该活动所需的特定权限

now following the above we ask permissions only when needed and only for the specific permission which is required by that activity

但是这些最佳实践没有说明权限的频率,即说明这些权限需要显示多少次或只能显示一次

现在我面临的问题是

1) 每次在应用程序中调用 Activity 时,权限都会弹出,这些发生在应用程序使用的特定会话中(如果在同一会话期间调用相同的 Activity,则一旦授予权限就不会调用该权限)

1) each time the activity is called in the app the permissions pop up, These happens for that particular session of app usage(if same activity is called during the same session the permission is not called for once it is granted)

2) 我们可以存储已经被授予的权限吗(即在共享首选项中执行第一次检查的静态变量,这样权限就不会被一次又一次地调用)

2) Can we store permissions that have been granted (ie the static variable which performs the first check in shared preference so that permissions are not called again and again)

3) 每次使用app时是否需要调用权限访问

4) 如果不允许共享首选项,我们如何更优雅地管理这些权限

4) If shared preferences are not allowed how to we manage these permissions more elegantly

推荐答案

1) 您引用的文档说:

1) The documentation you referenced says:

如果您的应用需要危险权限,您必须检查您是否每次执行需要的操作时都拥有该权限那个权限.用户始终可以自由撤销权限,因此即使应用程序昨天使用了相机,它也不能假设它仍然存在今天有那个权限.

If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can't assume it still has that permission today.

这就是您每次都必须请求它的原因.

That's why you have to request it every time.

2) 您不能存储权限.Android 决定记住授予的权限多长时间.

2) You can't store permissions. Android decides how long to remember a granted permissions.

3) 是的,这就是上面引用的意思.

3) Yes, that's what the above quote is saying.

4) 您让 Android 管理权限.除了在需要时检查或请求它们之外,您不会自己做任何事情.

4) You let Android manage the permissions. You don't do anything yourself except check or request them when you need them.

这篇关于Android 6.0 中的 Android 运行时权限(API 级别 23)最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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