How can I access a private Android API that does not appear in TelephonyManager?

I intend to write tests related to recording a phone and a direct SIM card.

What are the alternatives if the required APIs are not displayed in TelephonyManager, but exist as private APIs in PhoneBase.java , PhoneFactory.java or CommandInterface.java ?

In particular, my questions are:

  • What is the "replacement" for: PhoneFactory.getDefaultPhone() ?
  • What is the alternative to access CommandsInterface (for example: CommandsInterface mCmdIf = ((PhoneBase)mPhone).mCM )?

Thanks in advance,
Micah

+4
source share
1 answer

You can use java reflection .

This is how I accessed the methods of the 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(); } 
+6
source

Source: https://habr.com/ru/post/1308968/


All Articles