gpt4 book ai didi

android - 在 Android 9 上绕过 android usb 主机权限确认对话框

转载 作者:行者123 更新时间:2023-12-04 02:45:38 26 4
gpt4 key购买 nike

在 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上运行?

最佳答案

我们可以通过输入来解决它:

adb shell
su
settings put global hidden_api_policy_pre_p_apps 1
settings put global hidden_api_policy_p_apps 1

对非 SDK 接口(interface)的限制 (Android 9): https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

然后 grantDevicePermission方法将通过在 Android 9 上的反射再次可用:
public static boolean grantAutomaticUsbPermissionRoot(Context context, UsbDevice usbDevice) {
try {
PackageManager pkgManager = context.getPackageManager();
ApplicationInfo appInfo = pkgManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getServiceMethod = serviceManagerClass.getDeclaredMethod("getService", String.class);
getServiceMethod.setAccessible(true);
android.os.IBinder binder = (android.os.IBinder)getServiceMethod.invoke(null, Context.USB_SERVICE);

Class iUsbManagerClass = Class.forName("android.hardware.usb.IUsbManager");
Class stubClass = Class.forName("android.hardware.usb.IUsbManager$Stub");
Method asInterfaceMethod = stubClass.getDeclaredMethod("asInterface", android.os.IBinder.class);
asInterfaceMethod.setAccessible(true);
Object iUsbManager=asInterfaceMethod.invoke(null, binder);

final Method grantDevicePermissionMethod = iUsbManagerClass.getDeclaredMethod("grantDevicePermission", UsbDevice.class, int.class);
grantDevicePermissionMethod.setAccessible(true);
grantDevicePermissionMethod.invoke(iUsbManager, usbDevice,appInfo.uid);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

p.s.您当然需要 root 并系统化您的应用程序(移至 /system/priv-app/ )

关于android - 在 Android 9 上绕过 android usb 主机权限确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57390558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com