我想为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;
}
}
答案 0 :(得分:4)
当然是。您可以在下面看到它如何实现的示例:
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;
}
}
答案 1 :(得分:4)
对于我的项目,我用这种方法制作了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,以便有时间实现所有请求权限功能。
答案 2 :(得分:2)
一次性获取多个权限很容易。
我希望下面的代码可以帮助您授予多个权限。
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);
}