仅要求 Android Marshmallow 的运行时权限?
Posted
技术标签:
【中文标题】仅要求 Android Marshmallow 的运行时权限?【英文标题】:Ask for runtime permission only for Android Marshmallow? 【发布时间】:2016-04-22 07:08:39 【问题描述】:我只想为 marshmallow 添加运行时权限,因为它会使我的应用程序在 android marshmallow 设备中崩溃,否则它会正常工作。是否可以一次添加多个权限?我已经编写了相机权限的代码,但是
我已经为单一权限编写了代码,并且工作正常。
private void requestpermissioncamera()
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_CAMERA)) permissionsNeeded.add("Camera");
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),PERMISSION_REQUEST_CODE);
return;
此功能用于检查天气许可是否被授予。
private boolean checkPermission()
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
if (result == PackageManager.PERMISSION_GRANTED)
return true;
else
return false;
【问题讨论】:
learn2crack.com/2015/10/android-marshmallow-permissions.html 【参考方案1】:当然是。您可以在下面看到它是如何实现的示例:
public String[] PERMISSIONS_STORAGE =
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE
;
public boolean verifyStoragePermissions(Activity activity, int REQUEST_EXTERNAL_STORAGE)
initialize(activity);
// Check if we have write permission
int permission =
ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED)
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
return false;
else
return true;
【讨论】:
【参考方案2】:对于我的项目,我使用这种方法制作了 Utils 类:
public static boolean checkAllAndRequestPermisions(Activity activity, int requestCode, LinkedList<String> permissionsList)
LinkedList<String> needPermissions = new LinkedList<>();
for(String permission : permissionsList)
if (ContextCompat.checkSelfPermission(activity, permission) != activity.getPackageManager().PERMISSION_GRANTED)
needPermissions.add(permission);
if (!needPermissions.isEmpty())
ActivityCompat.requestPermissions(activity, needPermissions.toArray(new String[needPermissions.size()]), requestCode);
return true;
并像这样实现 onRequestPermissinsResult 接口:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case Consts.SOME_PERMISSIONS_REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
else
Toast.makeText(getApplicationContext(), "Need permissions", Toast.LENGTH_LONG).show();
return;
// other 'case' lines to check for other
// permissions this app might request
它不是完美的解决方案,但第一次可以,我希望它的帮助。
另外别忘了,你可以在 gradle targetSdkVersion 中设置小于 23 以有时间实现所有请求权限功能。
【讨论】:
【参考方案3】:一次获得多个权限很容易。
我希望下面的代码可以帮助您授予多个权限。
private void requestpermissioncamera()
List<String> permissionsNeeded = new ArrayList<String>();
final List<String> permissionsList = new ArrayList<String>();
if (!addPermission(permissionsList, Manifest.permission.ACCESS_FINE_LOCATION))
permissionsNeeded.add("GPS");
if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
permissionsNeeded.add("Read Contacts");
if (!addPermission(permissionsList, Manifest.permission.WRITE_CONTACTS))
permissionsNeeded.add("Write Contacts");
if(!addPermission(permissionsList,Manifest.permission.CAMERA))
permissionsNeeded.add("Camera");
if(!addPermission(permissionsList, Manifest.permission.ACCESS_NOTIFICATION_POLICY))
permissionsNeeded.add("push Notification");
if (permissionsList.size() > 0)
if (permissionsNeeded.size() > 0)
// Need Rationale
String message = "You need to grant access to " + permissionsNeeded.get(0);
for (int i = 1; i < permissionsNeeded.size(); i++)
message = message + ", " + permissionsNeeded.get(i);
showMessageOKCancel(message,new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
);
return;
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
return;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
switch (requestCode)
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_NOTIFICATION_POLICY, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED)
// All Permissions Granted
// insertDummyContact();
else
// Permission Denied
Toast.makeText(MainActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
【讨论】:
以上是关于仅要求 Android Marshmallow 的运行时权限?的主要内容,如果未能解决你的问题,请参考以下文章
在 Genymotion 中测试打盹功能(Android 6.0 Marshmallow)