我需要同时询问多个权限,但不会在marshmallow及更高版本中显示对话框。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private void RequestMultiplePermission() {
// Creating String Array with Permissions.
ActivityCompat.requestPermissions(MainActivity.this, new String[]
{
READ_SMS,
RECEIVE_SMS,
READ_PHONE_STATE,
PROCESS_OUTGOING_CALLS,
INTERNET,
RECEIVE_BOOT_COMPLETED,
READ_EXTERNAL_STORAGE,
WRITE_EXTERNAL_STORAGE
}, RequestPermissionCode);
}
// Calling override method.
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean ReadSMS = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean ReceiveSms = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean phoneState = grantResults[2] == PackageManager.PERMISSION_GRANTED;
boolean outGoingCall = grantResults[3] == PackageManager.PERMISSION_GRANTED;
boolean internet = grantResults[4] == PackageManager.PERMISSION_GRANTED;
boolean bootComplete = grantResults[5] == PackageManager.PERMISSION_GRANTED;
boolean readExternalStorage = grantResults[6] == PackageManager.PERMISSION_GRANTED;
boolean WriteExternalStorage = grantResults[7] == PackageManager.PERMISSION_GRANTED;
if (ReadSMS && ReceiveSms && phoneState && outGoingCall && internet && bootComplete && readExternalStorage && WriteExternalStorage) {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean CheckingPermissionIsEnabledOrNot() {
int FirstPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_SMS);
int SecondPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECEIVE_SMS);
int ThirdPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_PHONE_STATE);
int ForthPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), PROCESS_OUTGOING_CALLS);
int FivePermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), INTERNET);
int sixPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), RECEIVE_BOOT_COMPLETED);
int sevenPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
int eightPermissionResult = ContextCompat.checkSelfPermission(getApplicationContext(), WRITE_EXTERNAL_STORAGE);
return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
ForthPermissionResult == PackageManager.PERMISSION_GRANTED &&
FivePermissionResult == PackageManager.PERMISSION_GRANTED &&
sixPermissionResult == PackageManager.PERMISSION_GRANTED &&
sevenPermissionResult == PackageManager.PERMISSION_GRANTED &&
eightPermissionResult == PackageManager.PERMISSION_GRANTED;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= 23) {
if (!CheckingPermissionIsEnabledOrNot()) {
RequestMultiplePermission();
// carry on the normal flow, as the case of permissions granted.
} else {
//other granted stuff
}
}
}
}
结果
我没有收到任何错误,但是对话框没有显示,我的用户必须手动转到应用并授予权限。
任何人都可以告诉我哪里错了吗?
为什么我们需要在manifest中声明权限,在运行时声明另一个权限?
提前致谢
答案 0 :(得分:0)
您的权限字符串应该类似于
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
确保导入import android.Manifest;
您要提出的权限也应该在manifest其他对话框中声明,不会显示。
答案 1 :(得分:0)
试试这个,这可以作为多重权限的可重用和完美。
无论您想要多个权限,请在下面使用我的可重用类:
Permissions.getPermissions(this, new String[]{
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_CODE);
我可重复使用的多个权限类:
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by niraj.sanghani on 10/26/2017.
*/
/**
* Check for permissions
*
* {@link android.Manifest.permission}
*/
public class Permissions {
public static void getPermissions(Context context, List<String> permissionList, int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permit(context, permissionList, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
public static void getPermissions(Context context, String[] permissionArray, int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permit(context, Arrays.asList(permissionArray), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private static void permit(final Context context, List<String> permissionList, final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) {
final List<String> permissionsNeeded = new ArrayList<String>();
for (String s : permissionList) {
if (!checkPermission(context, s)) {
permissionsNeeded.add(s);
}
}
if (permissionsNeeded.size() > 0) {
// Need Rationale
String message = "You need to grant access ...";
showMessageOKCancel(context, message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, permissionsNeeded.toArray(new String[permissionsNeeded.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
});
return;
}
//return;
}
@RequiresApi(api = Build.VERSION_CODES.M)
private static boolean checkPermission(Context context, /*List<String> permissionsNeeded,*/ String permission) {
if (context.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
//permissionsNeeded.add(permission);
// Check for Rationale Option
if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission))
return false;
}
return true;
}
private static void showMessageOKCancel(Context context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}
答案 2 :(得分:0)
如果您尚未在清单中声明权限,则不会显示权限请求对话框。
此外,如果之前已授予所有请求的权限,则不会显示该对话框。只会请求先前未授予的权限。控件将直接转到方法onRequestPermissionResult
。要验证它,您可以卸载并重新安装该应用程序,然后将显示该对话框。
您需要在清单中声明权限,因为这是android系统的构建方式。在添加运行时权限之前,用户需要在安装期间授予所有权限,以便系统可以通过解析清单文件来显示所需的权限。
同样在应用信息的权限部分内,权限将从清单中显示。
您可以使用我的库轻松处理单个和多个运行时权限: