Android 6.0 multiple permissions(Android 6.0 多权限)
问题描述
我知道 Android 6.0 有新的权限,我知道我可以用类似这样的方式调用它们
I know that Android 6.0 has new permissions and I know I can call them with something like this
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, PERMISSION_WRITE_STORAGE);
}
今天我看到一个需要 3 个权限的 Google 应用:联系人、短信和相机.它正在制作一个页面 1-3 并同时将它们全部调用以激活.
Today I saw a Google app which needs 3 permissions: contacts, sms and camera. It's making a page 1-3 and calls them all together at the same time to activate.
谁能告诉我如何调用 4 个权限同时激活,比如短信、相机、联系人和存储?
Can anybody tell me how I can call 4 permissions to activate at the same time like sms, camera, contacts and storage?
示例(忘记了 google 应用的名称:( )
该应用需要短信、联系人和相机
Example (forgot the name of the google app :( )
The app needs sms,contacts and camera
应用程序询问我(并在第 1-3 页创建了一个对话框)激活短信、激活联系人和相机.所以这个谷歌应用程序将所有 3 个必需的权限一起调用,我的问题是我怎样才能获得相同的权限?
the app asked me (and made a dialog page1-3) activate sms, activate contacts and then camera. So this google app was calling all 3 required permissions together and my question is how can i achive the same ?
推荐答案
只需在 ActivityCompat.requestPermissions(...)
调用中包含所有 4 个权限,Android 就会像您提到的那样自动将它们分页在一起.
Just include all 4 permissions in the ActivityCompat.requestPermissions(...)
call and Android will automatically page them together like you mentioned.
我有一个辅助方法来检查多个权限,看看是否有任何一个没有被授予.
I have a helper method to check multiple permissions and see if any of them are not granted.
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
或者在 Kotlin 中:
Or in Kotlin:
fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
然后将所有权限发送给它.Android 只会询问它需要的那些.
Then just send it all of the permissions. Android will ask only for the ones it needs.
// The request code used in ActivityCompat.requestPermissions()
// and returned in the Activity's onRequestPermissionsResult()
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
android.Manifest.permission.READ_CONTACTS,
android.Manifest.permission.WRITE_CONTACTS,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_SMS,
android.Manifest.permission.CAMERA
};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
这篇关于Android 6.0 多权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 6.0 多权限
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01