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

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

问题描述

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)考虑使用意图

2)仅询问您需要的权限

2) Only Ask for Permissions You Need

3)不要让用户不知所措

3) Don't Overwhelm the User

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)每次在应用程序中调用活动时,都会弹出权限,这会在应用程序使用的特定会话中发生(如果在同一会话中调用了相同活动,则授予权限后就不会再调用该权限)

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)我们每次使用该应用程序时都需要调用权限访问权限吗

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

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