title: Calling a method from a system service
body: <p>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.</p>
  <pre><code>import android.service.persistentdata.PersistentDataBlockManager;
  </code></pre>
  <p>Writing the following line to the code will result in an error: Android Studio cannot resolve the symbol &quot;PersistentDataBlockManager&quot;, as its default</p>
  <blockquote>
  <p>android.service.*</p>
  </blockquote>
  <p>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:</p>
  <pre><code>Class&lt;?&gt; referenceClass = Class.forName(&quot;android.service.persistentdata.PersistentDataBlockManager&quot;);
  Object referenceObject = referenceClass.newInstance();
  </code></pre>
  <p>But instantiating an object threw an error: the class has no zero argument constructor. After checking its <a href="https://android.googlesource.com/platform/frameworks/base/+/1c4d535d0806dbeb6d2fa5cea0373cbd9ab6d33b/core/java/android/service/persistentdata/PersistentDataBlockManager.java" rel="nofollow noreferrer">source code</a> I found out that its only constructor looks like this:</p>
  <pre><code>public PersistentDataBlockManager(IPersistentDataBlockService service)
  </code></pre>
  <p>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:</p>
  <pre><code>context.getSystemService()
  </code></pre>
  <p>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.</p>
  <p>Is there a reliable way to call a non-static method from a non-default system service?</p>