Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access setPreferredNetworkType in Android source

I have a question for you, I am trying to select the Preferred Network Type on my Android phone. As you can do by doing following steps:

  1. Dial ##4636##
  2. Choose "Phone Information"
  3. Go bottom
  4. Choose preferred Network Type on menu

So after some searches on the source code I found the right class: Phone.java in (\frameworks\base\telephony\java\com\android\internal\telephony)

So with the nice tips of Vinay: How to disable Mobile Data on Android Who is using java reflexion to acces to hidden classes, I tried too by doing:

Method setPrefNetmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;

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("setPreferredNetworkType",new Class[] { Integer.class, Message.class });

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

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

But the problem is that I have this error on DDMS:

03-25 18:18:45.937: WARN/System.err(2989): java.lang.NoSuchMethodException: setPreferredNetworkType 03-25 18:18:45.937: WARN/System.err(2989): at java.lang.ClassCache.findMethodByName(ClassCache.java:308)

So do you have an idea to access setPreferredNetworkType or choose programmaticaly my preferred network type ?

For information (In RILConstants.java) :

/* NETWORK_MODE_* See ril.h RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE */
int NETWORK_MODE_WCDMA_PREF     = 0; /* GSM/WCDMA (WCDMA preferred) */
int NETWORK_MODE_GSM_ONLY       = 1; /* GSM only */
int NETWORK_MODE_WCDMA_ONLY     = 2; /* WCDMA only */
int NETWORK_MODE_GSM_UMTS       = 3; /* GSM/WCDMA (auto mode, according to PRL)
                                        AVAILABLE Application Settings menu*/
int NETWORK_MODE_CDMA           = 4; /* CDMA and EvDo (auto mode, according to PRL)
                                        AVAILABLE Application Settings menu*/
int NETWORK_MODE_CDMA_NO_EVDO   = 5; /* CDMA only */
int NETWORK_MODE_EVDO_NO_CDMA   = 6; /* EvDo only */
int NETWORK_MODE_GLOBAL         = 7; /* GSM/WCDMA, CDMA, and EvDo (auto mode, according to PRL)
                                        AVAILABLE Application Settings menu*/
int PREFERRED_NETWORK_MODE      = NETWORK_MODE_WCDMA_PREF;
like image 429
snakeman Avatar asked Mar 25 '11 17:03

snakeman


1 Answers

Here's a nice guide how to access the internal API: https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/

From own experience I can tell you that the access generally works but you will get a runtime exception because only system apps have permissions for setting Preferred Network Type.

This means your app also has to be installed in the system folder AND has to be signed with system key, which is the crux of the matter....

like image 146
Noogler Avatar answered Oct 03 '22 23:10

Noogler