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