permission.android.MANAGE_USB 在 Jellybean 中有效,但在 Lollipop 中无效 OS
permission.android.MANAGE_USB works in Jellybean but not in Lollipop OS
我正在开发一个需要此权限的应用程序: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.
有人知道为什么它在 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>
我会尝试将 android:protectionLevel
更改为 signatureOrSystem
:
A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission. Please avoid using this option, as the signature protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The "signatureOrSystem" permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.
对于signature
:
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
据我了解,您无权访问编译您在设备中使用的 Android OS 映像的任何人使用的签名密钥,因此 signature
protectionLevel
将不匹配且不授予您权限。
见
https://developer.android.com/guide/topics/manifest/permission-element.html
更新:为了完整起见,我在答案中添加@carlo.S评论:
In Lollipop, an app to be considered as a system application needs to be installed under the system/priv-app
folder and not system/app
folder like in Android 4.2.2! Also, app needs to have 644 permissions.
当然,设备需要root。
我正在开发一个需要此权限的应用程序: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.
有人知道为什么它在 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>
我会尝试将 android:protectionLevel
更改为 signatureOrSystem
:
A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission. Please avoid using this option, as the signature protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The "signatureOrSystem" permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.
对于signature
:
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
据我了解,您无权访问编译您在设备中使用的 Android OS 映像的任何人使用的签名密钥,因此 signature
protectionLevel
将不匹配且不授予您权限。
见 https://developer.android.com/guide/topics/manifest/permission-element.html
更新:为了完整起见,我在答案中添加@carlo.S评论:
In Lollipop, an app to be considered as a system application needs to be installed under the
system/priv-app
folder and notsystem/app
folder like in Android 4.2.2! Also, app needs to have 644 permissions.
当然,设备需要root。