在AndroidManifest.xml文件中,需要确保应用程序请求的所有权限都是合理的和必要的,包括敏感权限。同时,在访问敏感数据或功能之前,应该向用户请求授权。
下面是一个请求摄像头权限并使用摄像头的示例:
...
// Check for camera permission
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
// Permission has already been granted, do camera related work
// ...
} else {
// Permission is not granted, request camera permission
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA);
}
...
// Handle permission request result
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CAMERA: {
// If request is cancelled, the results array is empty
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, do camera related work
// ...
} else {
// Permission denied, handle accordingly
// ...
}
return;
}
// Handle other permission requests if needed
// ...
}
}