android - 我可以要求系统重新加载我的应用程序权限吗?

标签 android android-permissions aidl

有没有办法以编程方式要求 Android 重新加载/重新授予我的应用程序权限,而无需重新安装应用程序或在非根设备上重新启动设备?

我为什么需要它?

我的应用程序 (ClientApp) 绑定(bind)到另一个应用程序 (ServiceApp) 在 AIDL 中声明的服务。 ServiceApp 是我无法修改的第三方应用程序,它声明了 ClientApp 需要绑定(bind)到其服务的权限,因此我将该权限添加到我的 ClientApp 的 list 中:

<uses-permission android:name="com.example.service.API" />

当我在安装 ClientApp 之前安装 ServiceApp 时,一切正常。但是,当我第一次安装 ClientApp 然后安装 ServiceApp 时,在 ClientApp 中对 ServiceApp 的服务调用 bindService 时出现异常:

java.lang.SecurityException: Not allowed to bind to service Intent { act=com.example.service.API pkg=com.example.service }

我的理解是,我的应用未获得请求的安装权限,因为它在安装 ServiceApp 之前未在系统中声明。我可以通过在 ClientApp 的 list 中声明 ServiceApp 的权限来在 Android < 5.0 上修复它,但由于安全原因,这在 Android 5.0+ 中是不可能的。

如果我能以某种方式从我的代码中重新加载权限(这显然发生在设备重启时),我就可以解决问题。

最佳答案

您应该检查权限是否被授予。如果被授予,您可以使用它,否则您可以将权限请求显示为弹出

相机权限示例:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
{
    // permission granted
}
else
{
    // ask permission to user
    ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.CAMERA}, Constants.PermissionRequestCodes.CAMERA_REQUEST);
}

关于android - 我可以要求系统重新加载我的应用程序权限吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53412792/

相关文章:

java - 如何解决aosp中不允许的明文

android - 为什么在我的设置中使用多个项目为 android 开发速度很慢?

android - 如何降级android gradle?

android - android的READ_LOGS和READ_CALL_LOG权限的区别

java - 使用aidl调用另一个应用程序中的服务方法

android - 在 L 预览中使用 Material 主题

android - 从最大拉伸(stretch)中停止空 ConstraintLayout

android - miui手机如何查看短信权限

android - 将一部分Android源代码(TextToSpeech)硬编码到应用中

android - AIDL 找不到内部类