我正在学习如何申请权限。现在我想使用相机硬件,我正在尝试请求此权限。我提到了Android开发者的官方网站,我使用了网站上发布的示例 我改变了一点,因为我需要Android Camera硬件。
清单文件不包含任何权限,我希望当我运行以下发布的代码时,“Toast”将显示,因为清单文件中没有添加任何权限。但是发生的事情是,当我运行App时它没有问 添加任何权限,也不会显示“Toast”。
请向我解释为什么会发生这种情况以及如何正确设置
码:
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
答案 0 :(得分:0)
使用以下代码打开对话框:
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1);
获取活动结果如下:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
更多信息:https://developer.android.com/training/permissions/requesting.html
答案 1 :(得分:0)
您正尝试在else部分中请求权限...即已授予权限时。
外部其他表示授予权限....请执行以下操作
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
} else{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
} else {
Toast.makeText(this, "permission is granted already", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
// here you need to request permission, or show explanation if needed
} else {
//we already have permission, no need to request
}
所以正确的代码如下所示:
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
}
答案 3 :(得分:0)
您是否在Android m设备中运行该应用程序?
仅在android marshmallow设备中询问权限以获取更多信息请参阅here
对于在较低版本中运行的其他Android设备,其他marshmallow你需要在android manefest中声明权限......
请注意,权限对话框不会出现在运行低于android-m
的版本的Android设备中答案 4 :(得分:0)
private boolean checkWriteExternalPermission() {
part = "Hi! Goodmorning, I'm fine."
list = [[H,i],[!],[_],[G,o,o,d,m,o,r,n,i,n,g],[,],[_],[I],['],[m],[_],[f,i,n,e],[.]]
}
How permission can be checked at runtime without throwing SecurityException?
答案 5 :(得分:0)
即使您尝试为Android M实施运行时权限模型,也需要在AndroidManifest.xml中列出这些权限。
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED){
//do something
}
else
requestPermissions(permissionsList,
MY_PERMISSIONS_REQUEST_CODE);
然后在用户操作后:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do something
} else{
//do some other thing
}
break;
}
}