我想初次打开APP就自动弹出是否授权相机摄像机或者电话等等......。反正就是触发Android代码进行授权。
1、判断是否授权
a、PackageManager.PERMISSION_GRANTED获取相机权限代码,自己举一例三。
b、ActivityCompat.checkSelfPermission(this,permission)检查应用是否拥有该权限。(上下文,权限)
c、例:
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA)) {
//判断是否以授权相机权限,没有则授权
..........
}
2、申请授权
a、ActivityCompat.requestPermissions(this,permission,请求码),申请授权。(上下文,权限,请求码)
c、例:
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},
WRITE_PERMISSION_REQ_CODE);
3,一次申请多个权限,直接上代码
//Build.VERSION.SDK_INT >= 23
private boolean checkPublishPermission(){
List<String> permissions = new ArrayList<>();
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
permissions.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CAMERA)) {
permissions.add(android.Manifest.permission.CAMERA);
}
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.RECORD_AUDIO)) {
permissions.add(android.Manifest.permission.RECORD_AUDIO);
}
if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_PHONE_STATE)) {
permissions.add(Manifest.permission.READ_PHONE_STATE);
}
if (permissions.size() != 0) {
ActivityCompat.requestPermissions(MainActivity.this,(String[]) permissions.toArray(new String[0]),WRITE_PERMISSION_REQ_CODE);
return false;
}
}
checkPublishPermission()
4、实际例子