• Android Permissions管理之用户拒绝授权


    Android Permissions管理之用户拒绝授权,在Marshmallow之前的安卓版本,应用的权限只需要注册一下,应用就会获取到,在Marshmallow之后,为了安全,全新的权限模型出现,每次使用对应权限的时候,都会弹出 是否让应用获取权限的弹框。如果,用户点击是,给应用授权,那么一切都这样美好的进行就行,如果点击否,拒绝应用的授权,那么一切不美丽的事情就这样开始了,除非用户去设置里面设置给应用权限,那样应用才会正常进行,但是,很遗憾,大部分用户都不会设置这样操作的,繁琐,麻烦。那么解决方案就是每次需要的时候给用户弹出是否给应用授权。

    代码如下:

    package com.stylingandroid.permissions;
    
    import android.Manifest;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final int REQUEST_CODE = 0;
        private static final String[] PERMISSIONS = new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.MODIFY_AUDIO_SETTINGS};
        private PermissionsChecker checker;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            checker = new PermissionsChecker(this);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            if (checker.lacksPermissions(PERMISSIONS)) {
                startPermissionsActivity();
            }
        }
    
        private void startPermissionsActivity() {
            PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
                finish();
            }
        }
    }
    

      

    package com.stylingandroid.permissions;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    
    public class PermissionsActivity extends AppCompatActivity {
        public static final int PERMISSIONS_GRANTED = 0;
        public static final int PERMISSIONS_DENIED = 1;
    
        private static final String EXTRA_PERMISSIONS = "com.stylingandroid.permissions.EXTRA_PERMISSIONS";
        private PermissionsChecker checker;
    
        public static void startActivityForResult(Activity activity, int requestCode, String... permissions) {
            Intent intent = new Intent(activity, PermissionsActivity.class);
            intent.putExtra(EXTRA_PERMISSIONS, permissions);
            ActivityCompat.startActivityForResult(activity, intent, requestCode, null);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_permissions);
    
            checker = new PermissionsChecker(this);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            String[] permissions = getPermissions();
    
            if (checker.lacksPermissions(permissions)) {
                requestPermissions(permissions);
            } else {
                allPermissionsGranted();
            }
        }
    
        private String[] getPermissions() {
            return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
        }
    
        private void allPermissionsGranted() {
            setResult(PERMISSIONS_GRANTED);
            finish();
        }
    
        private void requestPermissions(String... permissions) {
            //NO-OP
        }
    
    }
    

      

    package com.stylingandroid.permissions;
    
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.support.v4.content.ContextCompat;
    
    class PermissionsChecker {
        private final Context context;
    
        public PermissionsChecker(Context context) {
            this.context = context;
        }
    
        public boolean lacksPermissions(String... permissions) {
            for (String permission : permissions) {
                if (lacksPermission(permission)) {
                    return true;
                }
            }
            return false;
        }
    
        private boolean lacksPermission(String permission) {
            return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
        }
    
    }
    

      

    from:https://github.com/StylingAndroid/Permissions

  • 相关阅读:
    C#调用C++代码遇到的问题总结
    ASP.NET MVC Controller的激活
    利用fis3自动化处理asp.net项目静态资源时遇到的一个编码问题
    工作中遇到的一个多线程下导致RCW无法释放的问题
    在mongoose中使用$match对id失效的解决方法
    passport源码研究
    android手机旋转屏幕时让GridView的列数与列宽度自适应
    git 版本回退
    梦之解读:如何成为牛人
    DataTable与DTO对象的简易转换类
  • 原文地址:https://www.cnblogs.com/spring87/p/5141861.html
Copyright © 2020-2023  润新知