【发布时间】:2019-12-14 20:44:14
【问题描述】:
在 Android 9 之前,我可以 bypass android usb host permission confirmation dialog 使用 root,系统化我的应用程序并使用下一个 USB 类 https://stackoverflow.com/a/15378118/7767664 https://stackoverflow.com/a/19681849/7767664
但它不适用于最新的 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: Exception) {
e.printStackTrace()
}
if (BuildConfig.DEBUG) Log.i(TAG, "permission was set usb device")
return true
}
} catch (e: PackageManager.NameNotFoundException) {
if (BuildConfig.DEBUG) e.printStackTrace()
} catch (e: RemoteException) {
if (BuildConfig.DEBUG) e.printStackTrace()
}
return false
}
有没有办法让它在 Android 9 上运行?
【问题讨论】:
-
您的“IUsbManager”似乎无法在 Android 9 上使用。可能“grantDevicePermission()”方法已被删除,或者它只是在较新的 Android 版本上具有不同的签名。
标签: android usb root android-usb