如何在运行时检查权限而不抛出SecurityException?

Emm*_*aux 87 security permissions android runtime

我设计了一个可以从SD获取/设置资源的函数,如果没有从sd中找到,则将其从Asset中取出,如果可能的话将资产写回SD.
如果SD已安装且可访问,则此函数可通过方法调用进行检查...

boolean bSDisAvalaible = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
Run Code Online (Sandbox Code Playgroud)

我设计的函数可以从一个app(项目)到另一个app(使用或不使用android.permission.WRITE_EXTERNAL_STORAGE)使用

然后我想检查当前应用程序是否具有此特定权限而不使用SecurityException.

它是否存在一种在运行时查询当前定义的权限的"好方法"?

ina*_*ruk 187

您可以使用Context.checkCallingorSelfPermission()此功能.这是一个例子:

private boolean checkWriteExternalPermission()
{
    String permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}
Run Code Online (Sandbox Code Playgroud)

  • @TalMihr为什么你认为`checkCallingOrSelfPermission()'会带来安全风险? (13认同)
  • 现在有一个可匹配性包装器:ContextCompat.checkSelfPermission (9认同)
  • 最好使用`checkCallingPermission()`而不是`checkCallingOrSelfPermission()`方法. (3认同)
  • 不可以.您不能在运行时给自己许可. (2认同)

use*_*321 51

这也是另一种解决方案

PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(
    android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
    context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
   // do stuff
}
Run Code Online (Sandbox Code Playgroud)


小智 18

你也可以用这个:

private boolean doesUserHavePermission()
{
    int result = context.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    return result == PackageManager.PERMISSION_GRANTED;
}
Run Code Online (Sandbox Code Playgroud)


ben*_*eni 11

像谷歌文档:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Run Code Online (Sandbox Code Playgroud)


hta*_*oya 8

分享我的方法以防有人需要它们:

 /** Determines if the context calling has the required permission
 * @param context - the IPC context
 * @param permissions - The permissions to check
 * @return true if the IPC has the granted permission
 */
public static boolean hasPermission(Context context, String permission) {

    int res = context.checkCallingOrSelfPermission(permission);

    Log.v(TAG, "permission: " + permission + " = \t\t" + 
    (res == PackageManager.PERMISSION_GRANTED ? "GRANTED" : "DENIED"));

    return res == PackageManager.PERMISSION_GRANTED;

}

/** Determines if the context calling has the required permissions
 * @param context - the IPC context
 * @param permissions - The permissions to check
 * @return true if the IPC has the granted permission
 */
public static boolean hasPermissions(Context context, String... permissions) {

    boolean hasAllPermissions = true;

    for(String permission : permissions) {
        //you can return false instead of assigning, but by assigning you can log all permission values
        if (! hasPermission(context, permission)) {hasAllPermissions = false; }
    }

    return hasAllPermissions;

}
Run Code Online (Sandbox Code Playgroud)

并称之为:

boolean hasAndroidPermissions = SystemUtils.hasPermissions(mContext, new String[] {
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.INTERNET,
        });
Run Code Online (Sandbox Code Playgroud)


box*_*box 5

您应该按以下方式检查权限(如此处所述的Android权限):

int result = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE);
Run Code Online (Sandbox Code Playgroud)

然后,将结果与以下任一项进行比较

result == PackageManager.PERMISSION_DENIED
Run Code Online (Sandbox Code Playgroud)

要么:

result == PackageManager.PERMISSION_GRANTED
Run Code Online (Sandbox Code Playgroud)