0

我正在学习如何请求权限。现在我想使用相机硬件,并且我正在尝试请求此权限。我提到Android开发人员的官方网站,我用网站上发布的例子,但 我改变了一点,因为我需要Android Camera硬件。权限未得到请求

清单文件不包含任何权限,我期望当我运行以下发布的代码时,“Toast”将显示,因为清单文件中没有添加任何权限。但是发生的是,当我运行应用程序时,它不会要求 添加任何权限,也不会显示“Toast”。

这是为什么happeneing请向我解释,以及如何设置正确

代码:使用下面的代码

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

打开一个对话框:

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

0

您正试图请求权限的其他部分...即权限已被授予。

外其他意味着权限被授予....做,而不是下面

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(); 
} 
+0

遵循@Amit Desale与多个权限 – Kushan

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); 
    } 
} 
+0

感谢..但打交道为什么if语句永远不会执行shouldShowRequestPermissionRationale的时候检查结果的答案?或换句话说,当它将被执行 – user2121

+0

它将不会执行,如果权限被拒绝并且用户选择不再显示这个按钮或者如果有人已经去了设置并且删除了权限。只有当权限被拒绝并且以上任何一项不成立时才会执行 – Kushan

0

你运行android m设备中的应用程序?

的权限要求只有在获得更多信息的Android设备棉花糖请参考here

对于其他的是棉花糖u需要在Android的manefest声明权限较低的版本上运行其他Android设备......

请注意,权限对话框不会出现在运行低于android-m的android设备上

+0

是的。我正在使用android M – user2121

0

你必须在AndroidManifest.xml中列出的权限,即使你想实现的Android M.运行权限模型

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; 

    } 
}