Android 6.0 - 申请动态权限
程序员文章站
2022-11-30 19:24:27
AndroidManifest.xml Java//定义请求码 private static final int...
- 本文只申请读写权限,举一反三,按照这个模版也可以申请其他权限。
AndroidManifest.xml ↓
<!--加读写权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Java ↓
//定义请求码
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
//然后通过一个函数来申请
public static void readWritePermission(Activity activity) {
try {
//检测是否有权限:permission = 0 ,则有权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
//判断是否有权限,没有则弹出对话框
if (permission != PackageManager.PERMISSION_GRANTED) {
//Activity的实例、String数组、请求码
ActivityCompat.requestPermissions(activity, PERMISSIONS,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//--------------在 onCreate中调用此函数--------------
readWritePermission(this);
效果图 ↓
本文地址:https://blog.csdn.net/weixin_43707799/article/details/107287193
上一篇: [SunnyLand从0到1]--开端
下一篇: 如果岳飞带兵打回临安,他会不会成功呢?
推荐阅读