单个权限推荐
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 100);
return;
}
多个权限:
//定义权限数组
public final String[]authBaseArr = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION
};
//申请
if (android.os.Build.VERSION.SDK_INT >=23) {
this.requestPermissions(authBaseArr, 100);
return;
}
//重写
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode ==100) {
Log.d("tag",grantResults.toString());
for (int ret : grantResults) {
if (ret ==0) {
continue;
}else {
Toast.makeText(MainActivity.this, "缺少导航基本的权限!", Toast.LENGTH_SHORT).show();
return;
}
}
}
}