在 Android 9 之前,我可以通过使用 root、系统化我的应用程序和使用下一个 USB 类来绕过 android usb 主机权限确认对话框/sf/answers/1076468291/ /sf/answers/1377729461/
但它不适用于最新的 Android 版本 - 9
它抛出 java.lang.NoSuchMethodError: No interface method grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V in class Landroid/hardware/usb/IUsbManager; or its super classes (declaration of 'android.hardware.usb.IUsbManager' appears in /system/framework/framework.jar)
fun setUsbPermissionRoot(device: UsbDevice) : Boolean {
if (BuildConfig.DEBUG) Log.i(TAG, "trying set permission")
try {
val pm = App.context.packageManager
val ai = pm.getApplicationInfo(App.context.packageName, 0)
ai?.let {
val b = ServiceManager.getService(Context.USB_SERVICE)
val service = IUsbManager.Stub.asInterface(b)
service.grantDevicePermission(device, it.uid)
try {
service.setDevicePackage(device, App.context.packageName, it.uid)
} catch (e: …
Run Code Online (Sandbox Code Playgroud)