How to use reflection to replace backup services?

I posted a question about the new behavior of Android 5.1, which disables the backup service when setting up the device owner on the device here ...

A possible solution could be (I think) to use reflection to fix the problem: I could find some examples of reflection using a specific method in a hidden class, but this case looks more complex, with a constructor that uses another hidden class (ServiceManager), etc. D. And I don’t know how to do it ...

The code that annoys me is in DevicePolicyManagerService.java (can be found here ) on lines 3739 through 3749:

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); } 

And my goal would be to turn on the backup service again, ideally it would call something like:

 ibm.setBackupServiceActive(UserHandle.USER_OWNER, false); 

Could you help me do this?

+7
android reflection
source share
1 answer

try this code:

  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); } 

He will need refactoring to detect errors ...

+3
source share

All Articles