private void startLogin() {
// 安卓6以后 都需要用户自己开启权限
if(hasWriteExternalStoragePermission()){
// 如果有这个权限 就去登陆
Login();
}else{
// 否则去申请权限
applyPermission();
}
}
private boolean hasWriteExternalStoragePermission() {
// 检查是否有写入权限
return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PermissionChecker.PERMISSION_GRANTED;
}
private void applyPermission() {
// 申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE);
}
/**
* 申请权限的回调
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_WRITE_EXTERNAL_STORAGE:
if (grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {
login();
} else {
toast(getString(R.string.not_get_permission));
}
break;
}
}