Search code examples
androidreflectiontelephonynexus-stelephonymanager

How to use getAvailableNetworks from android/internal/telephony/Phone.java


I have a requirement where I need to develop an Android application for Nexus S to get all the available network operators surrounding me.

I have been following up with lot of blogs to get this done.

Firstly it is an internal/hidden API

So I have followed this method to call it.

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

setPrefNetmethod = ITelephonyClass.getDeclaredMethod(
                    "getAvailableNetworks", new Class[] { Message.class });

Message response = Message.obtain();
setPrefNetmethod.setAccessible(false);

setPrefNetmethod.invoke(ITelephonyStub, new Object[] { network_mode, response });

But I get setPrefNetmethod = null always...

I have even set <uses-permission android:name="android.permission.READ_PHONE_STATE" /> in the android manifest...

Any help would be great for me.


Solution

  • Just to see what methods are available on the ITelephony object, instead of using getDeclaredMethod() try this:

    Method[] methods = ITelephonyClass.getDeclaredMethods();
    for (Method method : methods) {
        Log.i("Method", "Method name is: "+method.getName());
    }
    

    This should log a list of the available methods. Maybe there is one that looks like it will help you. Once you find the method you want, you can also call method.getParameterTypes() which will return an array of class names. This will tell you what parameters the method expects.

    However, it may not be possible for you to do what you want, as each manufacturer is free to change this stuff as they see fit.