android程序启动即开启动态权限

android,尽管我们在AndroidManifest里面申请了权限加载,但是在使用过称中,发现这些权限还是没有,什么原因呢?因为android在6.0版本以后,我们需要动态加载它,今天带领小伙伴们看看怎么在android6.0程序的入口处写动态加载权限吧。
首先我们定义一个Avtivity作为程序的第一个入口WelcomeActivity:
定义一个权限数组:
String[] permissions = new String[]{
      android.Manifest.permission.ACCESS_COARSE_LOCATION,
        android.Manifest.permission.ACCESS_FINE_LOCATION,
      android.Manifest.permission.WRITE_EXTERNAL_STORAGE,      android.Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
        android.Manifest.permission.BLUETOOTH,
        android.Manifest.permission.BLUETOOTH_ADMIN,
        android.Manifest.permission.BLUETOOTH_PRIVILEGED,
};
 List mPermissionList = new ArrayList<>();
boolean mShowRequestPermission = true;//用户是否禁止权限
 然后写一个方法checkPermision()检查程序是否已开启本权限
   private void checkPermision() {
    mPermissionList.clear();
    for (int i = 0; i < permissions.length; i++) {
      if (ContextCompat.checkSelfPermission(WelcomeActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
            mPermissionList.add(permissions[i]);
        }
    }
     /**
             * 判断是否为空
             */
         if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予      了
        initLoading();
    } else {//请求权限方法
        String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
        ActivityCompat.requestPermissions(WelcomeActivity.this, permissions, 1);
    }
    }
    //开启线程跳转
 private void initLoading() {
    StatusBarUtil.setTransparent(WelcomeActivity.this);
    tv = (TextView) findViewById(R.id.textView1);
    mc = new MyCountDownTimer(3000, 1000);
    mc.start();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(WelcomeActivity.this, LoginsActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);
}

private Handler handler = new Handler();
class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    public void onFinish() {
        tv.setText("正在跳转");
    }

    public void onTick(long millisUntilFinished) {
        tv.setText("倒计时(" + millisUntilFinished / 1000 + ")");
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    //判断是否勾选禁止后不再询问
                    boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this, permissions[i]);
                    if (showRequestPermission) {//
                        checkPermision();//重新申请权限
                        return;
                    } else {
                        mShowRequestPermission = false;//已经禁止
                    }
                }
            }
            initLoading();
            break;
        default:
            break;
    }
}

}
看下效果图吧:第二个完成了就跳转界面了:
android程序启动即开启动态权限_第1张图片
android程序启动即开启动态权限_第2张图片

共同学习可以加我QQ好友哦(971873592)

你可能感兴趣的:(android,java)