在低于棉花糖的Android版本中,我可以运行将文件写入外部存储的应用程序。在这些系统中,授权在安装应用程序时被授予。但是当我试图在棉花糖中运行我的应用程序时,它在安装时说“该应用程序不需要任何权限”。当我执行写入功能时,应用程序意外退出。我们是否需要在Marshmallow中明确要求AndroidManifest.xml以外的权限?
通常,设备会在首次打开时要求为每个应用授予权限。但在我的应用程序中,这也不会发生。
在低于棉花糖的Android版本中,我可以运行将文件写入外部存储的应用程序。在这些系统中,授权在安装应用程序时被授予。但是当我试图在棉花糖中运行我的应用程序时,它在安装时说“该应用程序不需要任何权限”。当我执行写入功能时,应用程序意外退出。我们是否需要在Marshmallow中明确要求AndroidManifest.xml以外的权限?
通常,设备会在首次打开时要求为每个应用授予权限。但在我的应用程序中,这也不会发生。
您必须自行为Android 6.0+(Marshmallow)提供运行时权限处理。有关更多信息,请参阅here。
在Android M及以上版本中,您必须要求分类为“危险”的权限。您可以找到需要请求的权限的完整列表here。
但是,您可以通过将compileSDK和targetSDK设置为<来避免请求。请注意,这会阻止您使用任何API 23+功能。
您所请求的权限是这样的:
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},//Your permissions here
1);//Random request code
检查用户是否正在运行的API 23+这样做:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Request: Use a method or add the permission asking directly into here.
}
如果你需要检查的结果,你可以做到这一点像这样:
@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
}
}
哇..谢谢.. !! –