[英]What should be my minimum/target sdk version for material design and external storage acess?
I want to write a very simple app that only has 2 requirements. 我想编写一个只有2个要求的非常简单的应用程序。 Support for material design and uses the phone internal storage.
支持材料设计并使用手机内部存储。
So when I created the Android studio project my minSdkVersion was set to 21 and my targetSdkVersion was set to 26. 因此,当我创建Android Studio项目时,我的minSdkVersion设置为21,而我的targetSdkVersion设置为26。
This gave me the following problem. 这给了我以下问题。 When I wanted to get access to internal storage I couldn't because of the new runtime permission system.
当我想访问内部存储时,由于新的运行时权限系统,我无法访问。 I figured I needed to write the code for requesting persmissions but then I run into the problem that the requestPermissions function requires a minSdkVersion of 23.
我认为我需要编写用于请求权限的代码,但是随后遇到了一个问题,即requestPermissions函数的minSdkVersion必须为23。
So, here is what I want. 所以,这就是我想要的。 I want to make sure that my app works in any android version higher than 5. What is the right way to solve this permission issue and what should my minSdk and targetSdk be?
我想确保我的应用程序可以在高于5的任何android版本中使用。解决此权限问题的正确方法是什么?minSdk和targetSdk应该是什么?
I want to set my minSdkVersion to 21 and be done with it, but I don't know what problems that might bring. 我想将minSdkVersion设置为21并完成它,但是我不知道会带来什么问题。
You should request the permissions using the Support Library. 您应该使用支持库请求权限。 The official documentation explains pretty well the entire process: Requesting Permissions at Run Time .
官方文档很好地解释了整个过程: 在运行时请求权限 。 As this guide says:
如本指南所述:
using the support library is simpler, since your app doesn't need to check which version of Android it's running on before calling the methods
使用支持库更简单,因为您的应用无需在调用方法之前检查其运行的Android版本
public boolean haveStoragePermission() {
if (Build.VERSION.SDK_INT >= 23) {
if (getApplicationContext().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
getApplicationContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(AddPostActivity.this , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
return true;
}
}
And add this method at the beginning of onCreate()
method and don't forget to add those two permission into mainifest file. 并在
onCreate()
方法的开头添加此方法,不要忘记将这两个权限添加到mainifest文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.