• PermissionUtils


    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.os.Build;
    import android.support.v4.content.ContextCompat;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * DESC 6.0 权限工具类
     * Created by douhaoqiang on 2017/2/27.
     */
    public class PermissionUtils {
        private static final String TAG = "PermissionUtils";
    
        private static int mRequestCode = -1;
    
        private static OnPermissionListener mOnPermissionListener;
    
        /**
         * 权限请求回调
         */
        public interface OnPermissionListener {
    
            //权限通过
            void onPermissionGranted();
    
            //权限拒绝
            void onPermissionDenied();
    
        }
    
        /**
         * 调用请求响应的权限
         * @param context     上下文菜单 必须为Activity
         * @param requestCode 请求码
         * @param permissions 请求权限
         * @param listener    权限请求监听
         */
        @TargetApi(Build.VERSION_CODES.M)
        public static void requestPermissions(Context context, int requestCode
                , String[] permissions, OnPermissionListener listener) {
            if (context instanceof Activity) {
                mOnPermissionListener = listener;
                List<String> deniedPermissions = getDeniedPermissions(context, permissions);
                if (deniedPermissions.size() > 0) {
                    mRequestCode = requestCode;
                    ((Activity) context).requestPermissions(deniedPermissions
                            .toArray(new String[deniedPermissions.size()]), requestCode);
    
                } else {
                    if (mOnPermissionListener != null)
                        mOnPermissionListener.onPermissionGranted();
                }
            } else {
                throw new RuntimeException("Context must be an Activity");
            }
        }
    
        /**
         * 获取请求权限中需要授权的权限
         */
        private static List<String> getDeniedPermissions(Context context, String... permissions) {
            List<String> deniedPermissions = new ArrayList<>();
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) {
                    deniedPermissions.add(permission);
                }
            }
            return deniedPermissions;
        }
    
        /**
         * 请求权限结果,对应Activity中onRequestPermissionsResult()方法。
         */
        public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            if (mRequestCode != -1 && requestCode == mRequestCode) {
                if (mOnPermissionListener != null) {
                    if (verifyPermissions(grantResults)) {
                        mOnPermissionListener.onPermissionGranted();
                    } else {
                        mOnPermissionListener.onPermissionDenied();
                    }
                }
            }
        }
    
        /**
         * 验证所有权限是否都已经授权
         */
        private static boolean verifyPermissions(int[] grantResults) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
    
    }
    

     使用:

    public class SplashActivity extends Activity {
    
    
        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO
    
        };
    
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
            setContentView(R.layout.activity_splash);
            requestPermissions();
    
        }
    
    
    
        public void requestPermissions() {
            PermissionUtil.requestPermissions(this, 1, permissions, new PermissionUtil.OnPermissionListener() {
    
                @Override
                public void onPermissionGranted() {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
    
                @Override
                public void onPermissionDenied() {
                    finish();
                }
            });
        }
    
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            PermissionUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    
        //隐藏虚拟导航栏按键
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus) {
                getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN
                );
            }
        }
    
    
    }
  • 相关阅读:
    python爬虫学习
    Java基础知识11--Optional类
    07 Windows访问远程共享文件夹---利用\IP地址
    Springcloud 学习笔记15-打开postman console控制台,查看接口测试打印log日志信息
    Springcloud 学习笔记14-IaaS, PaaS和SaaS的区别
    Springcloud 学习笔记13-使用PostMan上传/下载文件,前后端联合测试
    Java基础知识10--Stream API详解02
    Java基础知识09--Stream API详解01
    洛谷 P2587 [ZJOI2008]泡泡堂(贪心)
    洛谷 P3199 [HNOI2009]最小圈(01分数规划,spfa判负环)
  • 原文地址:https://www.cnblogs.com/loaderman/p/8269013.html
Copyright © 2020-2023  润新知