绕过android 9上的android usb主机权限确认对话框

绕过android 9上的android usb主机权限确认对话框,android,usb,root,android-usb,Android,Usb,Root,Android Usb,在安卓9之前,我可以通过使用root,系统化我的应用程序和使用nextusb类 但它不适用于最新的Android版本-9 它在类Landroid/hardware/usb/IUsbManager中抛出java.lang.NoSuchMethodError:No接口方法grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V;或者它的超类(声明'android.hardware.usb.IUsbManager'出现在/system/fram

在安卓9之前,我可以通过使用root,系统化我的应用程序和使用nextusb类

但它不适用于最新的Android版本-9

它在类Landroid/hardware/usb/IUsbManager中抛出
java.lang.NoSuchMethodError:No接口方法grantDevicePermission(Landroid/hardware/usb/UsbDevice;I)V;或者它的超类(声明'android.hardware.usb.IUsbManager'出现在/system/framework/framework.jar中)


有什么方法可以让它在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接口(Android 9)的限制:

然后,
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、 当然,你需要根目录并系统化你的应用程序(移动到
/system/priv-app/

你的“IUsbManager”似乎不能在Android 9上使用。可能“grantDevicePermission()”方法已被删除,或者它在较新的Android版本上只是具有不同的签名。@Hoseinhaqian是的,应该是root还是签名的SYSTEM应用程序?或者只是根用户而不是已签名的系统应用程序?android 10上的这项工作?@Fortran它可以是调试签名的应用程序,不是吗matter@sounds很好,未签名的系统apk运行良好,我正确理解?
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;
    }
}