检查Android中的用户是不是已经授予权限
Posted
技术标签:
【中文标题】检查Android中的用户是不是已经授予权限【英文标题】:Checking if permissions have been granted already by user in Android检查Android中的用户是否已经授予权限 【发布时间】:2017-02-09 17:35:35 【问题描述】:我已经在一个字符串数组中定义了所有危险的权限,如下所示:
String[] perms = Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.MODIFY_PHONE_STATE;
然后检查他们是否被授予我运行这个:
for (int i = 0; i < perms.length; i++)
if(ContextCompat.checkSelfPermission(this,perms[i])!=PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,perms, permsRequestCode);
break;
由于某种原因,这不起作用。它请求权限一次,然后如果我从设置中手动禁用它,它会多次弹出对话框。
我该如何解决这个问题?
【问题讨论】:
没有一个答案引用了该文档,可能是因为它以前不存在。如果您想要权威文档,请联系here。 【参考方案1】:据我所知,这应该有效。但是您可以尝试使用以下有效的功能。它的方法与你的不同。
String requiredPermission = android.Manifest.permission.READ_CONTACTS;
int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);
现在你可以检查了:
if (checkVal==PackageManager.PERMISSION_GRANTED)
【讨论】:
我建议不要将 requiredPermission 设置为字符串文字,而是将其设置为 Manifest.permission.READ_CONTACTS 推荐理由是什么? @knjk04 如果有人想检查 Manifest.permission 没有的权限怎么办,比如“com.qualcomm.permission...”【参考方案2】:使用它来检查您想要的任何权限,一次或多个权限。
public class PermissionsUtils
public static final int REQUEST_PERMISSION_MULTIPLE = 0;
public static final int REQUEST_PERMISSION_CAMERA = 1;
public static final int REQUEST_PERMISSION_LOCATION = 2;
public static final int REQUEST_WRITE_EXTERNAL = 3;
public static boolean checkAndRequestPermissions(Activity activity)
System.out.println("PermissionsUtils checkAndRequestPermissions()");
int permissionCamera = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
int permissionLocation = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
int permissionWriteExternal = ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
// Permission List
List<String> listPermissionsNeeded = new ArrayList<>();
// Camera Permission
if (permissionCamera != PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA))
Toast.makeText(activity, "Camera Permission is required for this app to run", Toast.LENGTH_SHORT)
.show();
listPermissionsNeeded.add(Manifest.permission.CAMERA);
// Read/Write Permission
if (permissionWriteExternal != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
// Location Permission
if (permissionLocation != PackageManager.PERMISSION_GRANTED)
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
if (!listPermissionsNeeded.isEmpty())
ActivityCompat.requestPermissions(activity,
listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
REQUEST_PERMISSION_MULTIPLE);
return false;
return true;
/**
* Requests the Camera permission. If the permission has been denied
* previously, a SnackBar will prompt the user to grant the permission,
* otherwise it is requested directly.
*/
public static void requestCameraPermission(Activity activity)
// Here, thisActivity is the current activity
// System.out.println("requestCameraPermission() INITIAL");
// Toast.makeText(this, "requestCameraPermission() INITIAL",
// Toast.LENGTH_LONG).show();
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA))
// Toast.makeText(activity, "Camera permission is
// needed for this app to run ",
// Toast.LENGTH_SHORT).show();
// System.out.println("requestCameraPermission() SHOW INFO");
// Show an explanation to the user *asynchronously* -- don't
// block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.CAMERA ,
REQUEST_PERMISSION_CAMERA);
else
// No explanation needed, we can request the permission.
// System.out.println("requestCameraPermission() ASK
// PERMISSION");
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.CAMERA ,
REQUEST_PERMISSION_CAMERA);
// Permission is granted
else
System.out.println("requestCameraPermission() PERMISSION ALREADY GRANTED");
public static void requestLocationPermission(Activity activity)
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_FINE_LOCATION))
Toast.makeText(activity, "LOCATION permission is needed to display location info ", Toast.LENGTH_SHORT)
.show();
// Show an explanation to the user *asynchronously* -- don't
// block this thread waiting for the user's response! After the
// user sees the explanation, try again to request the
// permission.
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.ACCESS_FINE_LOCATION ,
REQUEST_PERMISSION_LOCATION);
Toast.makeText(activity, "REQUEST LOCATION PERMISSION", Toast.LENGTH_LONG).show();
else
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.ACCESS_FINE_LOCATION ,
REQUEST_PERMISSION_LOCATION);
Toast.makeText(activity, "REQUEST LOCATION PERMISSION", Toast.LENGTH_LONG).show();
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
// Permission is granted
else
public static void requestWriteExternalPermission(Activity activity)
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE))
Toast.makeText(activity, "Write permission is needed to create Excel file ", Toast.LENGTH_SHORT).show();
// Show an explanation to the user *asynchronously* -- don't
// block this thread waiting for the user's response! After the
// user sees the explanation, try again to request the
// permission.
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.WRITE_EXTERNAL_STORAGE ,
REQUEST_WRITE_EXTERNAL);
Toast.makeText(activity, "REQUEST LOCATION PERMISSION", Toast.LENGTH_LONG).show();
else
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[] Manifest.permission.WRITE_EXTERNAL_STORAGE ,
REQUEST_WRITE_EXTERNAL);
public static boolean hasPermissions(Context context, String... permissions)
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
for (String permission : permissions)
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
return false;
return true;
【讨论】:
这个答案完全是题外话 @mahdiazarm。这怎么跑题了?checkSelfPermission(activity, Manifest.permission.X)
检查是否已授予任何权限,如果您查看其他答案,他们也会这样做,其余代码会要求未授予的权限。
@Tracian 您的其余代码无用,与主题无关。
@mahdiazarm 这是你的意见。许多其他人会发现该示例很有用,而附加上下文也很有帮助【参考方案3】:
尝试在String[]
中添加perms
它对我有用..
for (int i = 0; i < perms.length; i++)
if(ContextCompat.checkSelfPermission(this,perms[i])!=PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this,String[]perms, permsRequestCode);
break;
【讨论】:
以上是关于检查Android中的用户是不是已经授予权限的主要内容,如果未能解决你的问题,请参考以下文章
如何知道用户之前是不是已经授予 Facebook SDK 权限
在 Xamarin android 代码中找不到“ACCESS_BACKGROUND_LOCATION”权限。我需要检查用户是不是已授予此权限
Android:检查是不是在另一个类中授予权限,而不是在 MainActivity
ReactNative:如何检查用户是不是已授予 gps 权限