zoukankan      html  css  js  c++  java
  • 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
                );
            }
        }
    
    
    }
  • 相关阅读:
    Application.Exit()结束程序,但线程还在的解决方法
    Myeclipse2014 SVN安装方法以及项目上传到svn服务器
    MyEclipse中把JSP默认编码改为UTF-8
    005_MyEclipse编码设置
    laravel5.2学习资源
    wechat-php-sdk
    微信邀请卡的开发
    微信JS-SDK实际分享功能
    每个Linux新手都应该记住的10个基本Linux命令
    Linux下定时备份数据库
  • 原文地址:https://www.cnblogs.com/loaderman/p/8269013.html
Copyright © 2011-2022 走看看