Permission.android.MANAGE_USB 在 Jellybean 中有效,但在 Lollipop OS 中无效

car*_*o.S 6 java permissions android android-4.1-jelly-bean android-5.1.1-lollipop

我正在开发一个需要此权限的应用程序:android.permission.MANAGE_USB


我知道此权限仅在系统应用程序中授予。

如果我在 android 4.1.2 中将该应用程序安装为系统应用程序,则该应用程序可以正常工作。

但是如果我尝试将它作为系统应用程序安装在 android 5.1 中,那么调试日志会打印出我没有权限:

错误:

W/System.err: java.lang.SecurityException: 
Neither user 10074 nor current process has android.permission.MANAGE_USB.
Run Code Online (Sandbox Code Playgroud)

有人知道为什么它在 android 5.1 中一直给我这个错误吗?

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">

<uses-permission android:name="android.permission.MANAGE_USB"
    android:protectionLevel="signature" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS"/>
<permission android:name="android.permission.SET_TIME"
android:protectionLevel="signatureOrSystem"
    />
<uses-feature android:name="android.hardware.usb.host"
    android:required="true"/>


<application
    android:name=".services.AppContext"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:hardwareAccelerated="false"
    android:theme="@style/AppTheme">
    <service android:name=".services.KioskService" android:exported="false"/>
    <activity
        android:name=".MainActivity"
        android:launchMode="singleInstance"
        android:stateNotNeeded="true"
        android:screenOrientation="sensorLandscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name="com.example.app.services.UsbService"
        android:enabled="true">
    </service>

    <receiver android:name=".services.PowerDisconnectReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
        </intent-filter>
    </receiver>

    <receiver android:name=".services.UsbPermissionReceiver">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
        </intent-filter>
    </receiver>

</application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

Xav*_*ana 5

我会尝试更改android:protectionLevelsignatureOrSystem

系统仅授予 Android 系统映像中的应用程序或使用与声明该权限的应用程序相同的证书签名的应用程序的权限。请避免使用此选项,因为签名保护级别应该足以满足大多数需求,并且无论应用程序安装在何处都可以正常工作。“signatureOrSystem”权限用于某些特殊情况,其中多个供应商将应用程序内置到系统映像中,并且需要明确共享特定功能,因为它们是一起构建的。

为了signature

仅当请求应用程序使用与声明该权限的应用程序相同的证书进行签名时,系统才会授予该权限。如果证书匹配,系统会自动授予权限,而无需通知用户或请求用户明确批准。

据我了解,您无权访问编译您在设备中使用的 Android 操作系统映像的人所使用的签名密钥,因此signature protectionLevel不会匹配,也不会向您授予权限。

请参阅 https://developer.android.com/guide/topics/manifest/permission-element.html

更新:为了完整起见,我在答案中添加了 @carlo.S 评论:

在 Lollipop 中,要被视为系统应用程序的应用程序需要安装在该system/priv-app文件夹下,而不是system/app像 Android 4.2.2 那样安装在文件夹下!此外,应用程序需要有 644 权限。

当然,设备需要root。

  • 没有办法,除非你是一个三字母政府机构。所以,这是不行的,你应该使用你自己的签名密钥。话虽这么说,请注意,我建议的保护级别是签名**或**系统,并且文档显示“系统仅向** Android 系统映像中的应用程序授予**的权限......” 。当您将应用程序作为系统应用程序安装在 root 设备中时(您说“我已将应用程序安装在具有 644 权限的 system/app 文件夹中”),无论签名不匹配,此_应该_都可以工作。 (2认同)