Micha Valach
Micha Valach

Reputation: 41

How can I access Android private API's which doesn't exposed in TelephonyManager?

I intend to write tests related to the Phone and Direct SIM write.

What are the alternatives in case the required APIs aren't exposed in TelephonyManager but exist as private APIs in PhoneBase.java, PhoneFactory.java or CommandInterface.java?

Specifically, my questions are:

  1. What is the "replacement" for: PhoneFactory.getDefaultPhone()?
  2. What is the alternative in order to access the CommandsInterface (e.g.: CommandsInterface mCmdIf = ((PhoneBase)mPhone).mCM)?

Thanks in advance,
Micha

Upvotes: 4

Views: 3638

Answers (1)

Vaibhav Jani
Vaibhav Jani

Reputation: 12548

You can use java reflection.

Here is how I accessed methods of com.android.internal.telephony.ITelephony class to block an incoming call ...

private void endCall() throws Exception {
      TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

      Class<?> c = Class.forName(tm.getClass().getName());
      Method m = c.getDeclaredMethod("getITelephony");
      m.setAccessible(true);
      ITelephony telephonyService = (ITelephony) m.invoke(tm);

      telephonyService.silenceRinger();
      telephonyService.endCall();
      Toast.makeText(context, "Call Ended !", Toast.LENGTH_SHORT).show();
}

Upvotes: 6

Related Questions