Android 6.0多重权限 [英] Android 6.0 multiple permissions
问题描述
我知道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?
示例(忘记谷歌应用程序的名称:()
该应用程序需要短信,联系人和相机
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 ?
推荐答案
只需在<$ c $中包含所有4个权限c> ActivityCompat.requestPermissions(...)调用和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;
}
然后只需发送所有权限即可。 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多重权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!