如何使用反射来改变备份服务?

我发布了一个关于Android 5.1的新行为的问题,该行为在设备上设置设备所有者时会禁用备份服务...

一个可能的解决方案可能是(我猜)使用反射来解决这个问题:我可以在隐藏类中找到一些使用特定方法的反射示例,但这种情况看起来更加复杂,使用另一个隐藏类(ServiceManager)的构造函数,等等,我不知道该怎么做...

令我讨厌的代码位于第3739至3749行的DevicePolicyManagerService.java(可在此处找到):

long ident = Binder.clearCallingIdentity();
try {
    IBackupManager ibm = IBackupManager.Stub.asInterface(
        ServiceManager.getService(Context.BACKUP_SERVICE));
    ibm.setBackupServiceActive(UserHandle.USER_OWNER, false);
} catch (RemoteException e) {
    throw new IllegalStateException("Failed deactivating backup service.", e);
} finally {
    Binder.restoreCallingIdentity(ident);
}

而且我的目标是重新启用备份服务,理想情况下这可以称为:

ibm.setBackupServiceActive(UserHandle.USER_OWNER, false);

你能帮我做这个吗?


试试这个代码:

    try {
        Class<?> iBackupManagerClass = Class.forName("android.app.backup.IBackupManager");
        Class<?> serviceManagerClass = Class.forName("android.os.ServiceManager");
        Class<?>[] classes = iBackupManagerClass.getDeclaredClasses();
        Class<?> stubClass = null;
        for (Class clazz : classes) {
            if (clazz.getSimpleName().equals("Stub")) {
                stubClass = clazz;
            }
        }

        Method setBackupServiceActiveMethod = iBackupManagerClass.getMethod("setBackupServiceActive", int.class, boolean.class);
        Method asInterfaceMethod = stubClass.getMethod("asInterface", IBinder.class);
        Method getServiceMethod = serviceManagerClass.getMethod("getService", String.class);

        Object ibm = asInterfaceMethod.invoke(null, getServiceMethod.invoke(null, "backup"));
        setBackupServiceActiveMethod.invoke(ibm, 0, false);

    } catch (ClassNotFoundException e) {
        Log.e("TEST", e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        Log.e("TEST", e.getMessage(), e);
    } catch (InvocationTargetException e) {
        Log.e("TEST", e.getMessage(), e);
    } catch (IllegalAccessException e) {
        Log.e("TEST", e.getMessage(), e);
    }

它将需要一些重构错误捕获...

链接地址: http://www.djcxy.com/p/26325.html

上一篇: How to use reflection to change backup service?

下一篇: join function of a numpy array composed of string