我发布了question有关Android 5.1的新行为,在设备上设置设备所有者时禁用了备份服务...
一个可能的解决方案可能是(我猜)使用反射来解决问题:我可以在隐藏类中使用特定方法找到一些反射示例,但这种情况看起来更复杂,使用另一个隐藏类的构造函数(ServiceManager)等我不知道怎么做......
令我烦恼的代码位于第3739至3749行的DevicePolicyManagerService.java(可以找到here)中:
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);
你能帮我这么做吗?
答案 0 :(得分:3)
试试这段代码:
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);
}
需要进行一些重构才能捕获错误...