lundi 14 janvier 2019

Calling a method from a system service

There are system and custom services in Android that cannot be referenced from Android Studio. Lets say, I want to call a getFlashLockState() method from a PersistentDataBlockManager service. Default approach is to add a service reference.

import android.service.persistentdata.PersistentDataBlockManager;

Writing the following line to the code will result in an error: Android Studio cannot resolve the symbol "PersistentDataBlockManager", as its default

android.service.*

does not contain a reference to persistentdata service. I tried using reflection to get the class by name and was able to get a reference like this:

Class<?> referenceClass = Class.forName("android.service.persistentdata.PersistentDataBlockManager");
Object referenceObject = referenceClass.newInstance();

But instantiating an object threw an error: the class has no zero argument constructor. After checking its source code I found out that its only constructor looks like this:

public PersistentDataBlockManager(IPersistentDataBlockService service)

Basically, it turned out to be only a wrapper that accepts an actual implementation of a class from a system library. I also tried using other kinds of reflections like:

context.getSystemService()

to get an actual reference of a running service, but none of them worked for me, as the class is not static and I cant find a way to get its instance.

Is there a reliable way to call a non-static method from a non-default system service?





Aucun commentaire:

Enregistrer un commentaire