Change mobile network mode (gsm, wcdma, auto)










12















I want to change the preferred network mode ie. gsm or wcdma or auto, programmatically, from code, on Android.



Is this possible, and if so how ?










share|improve this question
























  • You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

    – JonasCz
    Mar 13 '16 at 17:04















12















I want to change the preferred network mode ie. gsm or wcdma or auto, programmatically, from code, on Android.



Is this possible, and if so how ?










share|improve this question
























  • You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

    – JonasCz
    Mar 13 '16 at 17:04













12












12








12


6






I want to change the preferred network mode ie. gsm or wcdma or auto, programmatically, from code, on Android.



Is this possible, and if so how ?










share|improve this question
















I want to change the preferred network mode ie. gsm or wcdma or auto, programmatically, from code, on Android.



Is this possible, and if so how ?







android android-networking telephony






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 13 '16 at 16:42









JonasCz

9,39052951




9,39052951










asked Apr 16 '12 at 7:29









Azhar ShaikhAzhar Shaikh

34.9k1281110




34.9k1281110












  • You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

    – JonasCz
    Mar 13 '16 at 17:04

















  • You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

    – JonasCz
    Mar 13 '16 at 17:04
















You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

– JonasCz
Mar 13 '16 at 17:04





You could look at the source code of the Android settings and see how it does it. I guess all you need is is the "Secure settings" permission, and then you can modify the value directly from your code. Not sure though..

– JonasCz
Mar 13 '16 at 17:04












3 Answers
3






active

oldest

votes


















3














Answer is NO



We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible.



We can develop something which will show current network and allow user short-cut from the app where they can switch network.






share|improve this answer























  • hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

    – Azhar Shaikh
    Apr 17 '12 at 4:07






  • 1





    I think just because of the reason : Security and User Privacy.

    – MKJParekh
    Apr 17 '12 at 4:34






  • 1





    is it not possible even on a ROOTed phone or using Android NDK?

    – Wojtek Owczarczyk
    Jun 1 '12 at 11:37











  • @WojtekO. I have no idea on tha.

    – MKJParekh
    Jun 1 '12 at 11:45






  • 2





    I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

    – rebel_UA
    Oct 31 '12 at 7:34


















8














It is possible, I did it.



For this to work, your app must be signed with the system key or have carrier privilege. Otherwise the app will throw
java.lang.SecurityException: No modify permission or carrier privilege.



My app runs on Android 5.1 Lollipop(API 22) and is signed with the system key, so this is the only configuration I can confirm for sure that works. I can't confirm the carrier privilege approach.



AndroidManifest.xml



Add this permission to your app manifest. If you are using Android Studio, it will probably mark this line as an error because only system apps can have this permission. If you can sign your app with the system keys, don't worry.



<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>


Get Preferred Network



The return is defined in RILConstants.java, e.g. RILConstants.NETWORK_MODE_WCDMA_PREF



public int getPreferredNetwork() 
Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
int preferredNetwork = -1000;
try
preferredNetwork = (int) method.invoke(mTelephonyManager);
Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
catch (IllegalAccessException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();


return preferredNetwork;



Set Preferred Method.



The parameter must be based on RILConstants.java, e.g.: RILConstants.NETWORK_MODE_LTE_ONLY



public void setPreferredNetwork(int networkType) 
try
Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
TelephonyManager.class, new Class int.class);
Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
networkType);
Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
catch (InvocationTargetException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();




This is an utility method to access the hidden API methods.



/**
* Get a hidden method instance from a class
* @param methodName The name of the method to be taken from the class
* @param fromClass The name of the class that has the method
* @return A Method instance that can be invoked
*/
public Method getHiddenMethod(String methodName, Class fromClass, Class params)
Method method = null;
try
Class clazz = Class.forName(fromClass.getName());
method = clazz.getMethod(methodName, params);
method.setAccessible(true);
catch (ClassNotFoundException e)
e.printStackTrace();
catch (NoSuchMethodException e)
e.printStackTrace();


return method;






share|improve this answer




















  • 1





    Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

    – BamsBamx
    Oct 26 '16 at 10:25











  • Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

    – Tulio F.
    Nov 22 '16 at 17:57











  • This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

    – not2qubit
    Jan 13 '17 at 6:29











  • I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

    – BamsBamx
    Jan 13 '17 at 11:14












  • @BamsBamx And is it on a stock, but rooted ROM?

    – not2qubit
    Jan 13 '17 at 15:03


















1














This is not an answer, but expanding on Tulio-F's answer.



The RILConstants.java contain the following:



// 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)**
int NETWORK_MODE_CDMA = 4; // CDMA and EvDo (auto mode, according to PRL)**
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)**
int NETWORK_MODE_LTE_CDMA_EVDO = 8; // LTE, CDMA and EvDo
int NETWORK_MODE_LTE_GSM_WCDMA = 9; // LTE, GSM/WCDMA
int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; // LTE, CDMA, EvDo, GSM/WCDMA
int NETWORK_MODE_LTE_ONLY = 11; // LTE Only mode.
int NETWORK_MODE_LTE_WCDMA = 12; // LTE/WCDMA
int NETWORK_MODE_TDSCDMA_ONLY = 13; // TD-SCDMA only
int NETWORK_MODE_TDSCDMA_WCDMA = 14; // TD-SCDMA and WCDMA
int NETWORK_MODE_LTE_TDSCDMA = 15; // TD-SCDMA and LTE
int NETWORK_MODE_TDSCDMA_GSM = 16; // TD-SCDMA and GSM
int NETWORK_MODE_LTE_TDSCDMA_GSM = 17; // TD-SCDMA,GSM and LTE
int NETWORK_MODE_TDSCDMA_GSM_WCDMA = 18; // TD-SCDMA, GSM/WCDMA
int NETWORK_MODE_LTE_TDSCDMA_WCDMA = 19; // TD-SCDMA, WCDMA and LTE
int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = 20; // TD-SCDMA, GSM/WCDMA and LTE
int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 21; // TD-SCDMA,EvDo,CDMA,GSM/WCDMA
int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; // TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo

int PREFERRED_NETWORK_MODE = SystemProperties.getInt("ro.telephony.default_network", NETWORK_MODE_WCDMA_PREF);


Where:



** = "AVAILABLE Application Settings menu"






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10170179%2fchange-mobile-network-mode-gsm-wcdma-auto%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Answer is NO



    We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible.



    We can develop something which will show current network and allow user short-cut from the app where they can switch network.






    share|improve this answer























    • hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

      – Azhar Shaikh
      Apr 17 '12 at 4:07






    • 1





      I think just because of the reason : Security and User Privacy.

      – MKJParekh
      Apr 17 '12 at 4:34






    • 1





      is it not possible even on a ROOTed phone or using Android NDK?

      – Wojtek Owczarczyk
      Jun 1 '12 at 11:37











    • @WojtekO. I have no idea on tha.

      – MKJParekh
      Jun 1 '12 at 11:45






    • 2





      I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

      – rebel_UA
      Oct 31 '12 at 7:34















    3














    Answer is NO



    We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible.



    We can develop something which will show current network and allow user short-cut from the app where they can switch network.






    share|improve this answer























    • hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

      – Azhar Shaikh
      Apr 17 '12 at 4:07






    • 1





      I think just because of the reason : Security and User Privacy.

      – MKJParekh
      Apr 17 '12 at 4:34






    • 1





      is it not possible even on a ROOTed phone or using Android NDK?

      – Wojtek Owczarczyk
      Jun 1 '12 at 11:37











    • @WojtekO. I have no idea on tha.

      – MKJParekh
      Jun 1 '12 at 11:45






    • 2





      I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

      – rebel_UA
      Oct 31 '12 at 7:34













    3












    3








    3







    Answer is NO



    We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible.



    We can develop something which will show current network and allow user short-cut from the app where they can switch network.






    share|improve this answer













    Answer is NO



    We can open directly the settings app of mobile network settings to switch between "2G" and "allow 3G" networks.A direct switch is sadly not possible.



    We can develop something which will show current network and allow user short-cut from the app where they can switch network.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 16 '12 at 7:33









    MKJParekhMKJParekh

    29.7k107894




    29.7k107894












    • hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

      – Azhar Shaikh
      Apr 17 '12 at 4:07






    • 1





      I think just because of the reason : Security and User Privacy.

      – MKJParekh
      Apr 17 '12 at 4:34






    • 1





      is it not possible even on a ROOTed phone or using Android NDK?

      – Wojtek Owczarczyk
      Jun 1 '12 at 11:37











    • @WojtekO. I have no idea on tha.

      – MKJParekh
      Jun 1 '12 at 11:45






    • 2





      I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

      – rebel_UA
      Oct 31 '12 at 7:34

















    • hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

      – Azhar Shaikh
      Apr 17 '12 at 4:07






    • 1





      I think just because of the reason : Security and User Privacy.

      – MKJParekh
      Apr 17 '12 at 4:34






    • 1





      is it not possible even on a ROOTed phone or using Android NDK?

      – Wojtek Owczarczyk
      Jun 1 '12 at 11:37











    • @WojtekO. I have no idea on tha.

      – MKJParekh
      Jun 1 '12 at 11:45






    • 2





      I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

      – rebel_UA
      Oct 31 '12 at 7:34
















    hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

    – Azhar Shaikh
    Apr 17 '12 at 4:07





    hmm, but there must be such mechanism. Dont know why Android havent gave priviledge of using such stuff.

    – Azhar Shaikh
    Apr 17 '12 at 4:07




    1




    1





    I think just because of the reason : Security and User Privacy.

    – MKJParekh
    Apr 17 '12 at 4:34





    I think just because of the reason : Security and User Privacy.

    – MKJParekh
    Apr 17 '12 at 4:34




    1




    1





    is it not possible even on a ROOTed phone or using Android NDK?

    – Wojtek Owczarczyk
    Jun 1 '12 at 11:37





    is it not possible even on a ROOTed phone or using Android NDK?

    – Wojtek Owczarczyk
    Jun 1 '12 at 11:37













    @WojtekO. I have no idea on tha.

    – MKJParekh
    Jun 1 '12 at 11:45





    @WojtekO. I have no idea on tha.

    – MKJParekh
    Jun 1 '12 at 11:45




    2




    2





    I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

    – rebel_UA
    Oct 31 '12 at 7:34





    I can't, but you should reverse engineering default settings app, install your app to /system/app/ and add WRITE_SECURE_SETTINGS to uses-permission. I don't know to why, but if you'll fun =)

    – rebel_UA
    Oct 31 '12 at 7:34













    8














    It is possible, I did it.



    For this to work, your app must be signed with the system key or have carrier privilege. Otherwise the app will throw
    java.lang.SecurityException: No modify permission or carrier privilege.



    My app runs on Android 5.1 Lollipop(API 22) and is signed with the system key, so this is the only configuration I can confirm for sure that works. I can't confirm the carrier privilege approach.



    AndroidManifest.xml



    Add this permission to your app manifest. If you are using Android Studio, it will probably mark this line as an error because only system apps can have this permission. If you can sign your app with the system keys, don't worry.



    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>


    Get Preferred Network



    The return is defined in RILConstants.java, e.g. RILConstants.NETWORK_MODE_WCDMA_PREF



    public int getPreferredNetwork() 
    Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
    int preferredNetwork = -1000;
    try
    preferredNetwork = (int) method.invoke(mTelephonyManager);
    Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
    catch (IllegalAccessException e)
    e.printStackTrace();
    catch (InvocationTargetException e)
    e.printStackTrace();


    return preferredNetwork;



    Set Preferred Method.



    The parameter must be based on RILConstants.java, e.g.: RILConstants.NETWORK_MODE_LTE_ONLY



    public void setPreferredNetwork(int networkType) 
    try
    Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
    TelephonyManager.class, new Class int.class);
    Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
    networkType);
    Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
    catch (InvocationTargetException e)
    e.printStackTrace();
    catch (IllegalAccessException e)
    e.printStackTrace();




    This is an utility method to access the hidden API methods.



    /**
    * Get a hidden method instance from a class
    * @param methodName The name of the method to be taken from the class
    * @param fromClass The name of the class that has the method
    * @return A Method instance that can be invoked
    */
    public Method getHiddenMethod(String methodName, Class fromClass, Class params)
    Method method = null;
    try
    Class clazz = Class.forName(fromClass.getName());
    method = clazz.getMethod(methodName, params);
    method.setAccessible(true);
    catch (ClassNotFoundException e)
    e.printStackTrace();
    catch (NoSuchMethodException e)
    e.printStackTrace();


    return method;






    share|improve this answer




















    • 1





      Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

      – BamsBamx
      Oct 26 '16 at 10:25











    • Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

      – Tulio F.
      Nov 22 '16 at 17:57











    • This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

      – not2qubit
      Jan 13 '17 at 6:29











    • I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

      – BamsBamx
      Jan 13 '17 at 11:14












    • @BamsBamx And is it on a stock, but rooted ROM?

      – not2qubit
      Jan 13 '17 at 15:03















    8














    It is possible, I did it.



    For this to work, your app must be signed with the system key or have carrier privilege. Otherwise the app will throw
    java.lang.SecurityException: No modify permission or carrier privilege.



    My app runs on Android 5.1 Lollipop(API 22) and is signed with the system key, so this is the only configuration I can confirm for sure that works. I can't confirm the carrier privilege approach.



    AndroidManifest.xml



    Add this permission to your app manifest. If you are using Android Studio, it will probably mark this line as an error because only system apps can have this permission. If you can sign your app with the system keys, don't worry.



    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>


    Get Preferred Network



    The return is defined in RILConstants.java, e.g. RILConstants.NETWORK_MODE_WCDMA_PREF



    public int getPreferredNetwork() 
    Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
    int preferredNetwork = -1000;
    try
    preferredNetwork = (int) method.invoke(mTelephonyManager);
    Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
    catch (IllegalAccessException e)
    e.printStackTrace();
    catch (InvocationTargetException e)
    e.printStackTrace();


    return preferredNetwork;



    Set Preferred Method.



    The parameter must be based on RILConstants.java, e.g.: RILConstants.NETWORK_MODE_LTE_ONLY



    public void setPreferredNetwork(int networkType) 
    try
    Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
    TelephonyManager.class, new Class int.class);
    Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
    networkType);
    Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
    catch (InvocationTargetException e)
    e.printStackTrace();
    catch (IllegalAccessException e)
    e.printStackTrace();




    This is an utility method to access the hidden API methods.



    /**
    * Get a hidden method instance from a class
    * @param methodName The name of the method to be taken from the class
    * @param fromClass The name of the class that has the method
    * @return A Method instance that can be invoked
    */
    public Method getHiddenMethod(String methodName, Class fromClass, Class params)
    Method method = null;
    try
    Class clazz = Class.forName(fromClass.getName());
    method = clazz.getMethod(methodName, params);
    method.setAccessible(true);
    catch (ClassNotFoundException e)
    e.printStackTrace();
    catch (NoSuchMethodException e)
    e.printStackTrace();


    return method;






    share|improve this answer




















    • 1





      Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

      – BamsBamx
      Oct 26 '16 at 10:25











    • Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

      – Tulio F.
      Nov 22 '16 at 17:57











    • This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

      – not2qubit
      Jan 13 '17 at 6:29











    • I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

      – BamsBamx
      Jan 13 '17 at 11:14












    • @BamsBamx And is it on a stock, but rooted ROM?

      – not2qubit
      Jan 13 '17 at 15:03













    8












    8








    8







    It is possible, I did it.



    For this to work, your app must be signed with the system key or have carrier privilege. Otherwise the app will throw
    java.lang.SecurityException: No modify permission or carrier privilege.



    My app runs on Android 5.1 Lollipop(API 22) and is signed with the system key, so this is the only configuration I can confirm for sure that works. I can't confirm the carrier privilege approach.



    AndroidManifest.xml



    Add this permission to your app manifest. If you are using Android Studio, it will probably mark this line as an error because only system apps can have this permission. If you can sign your app with the system keys, don't worry.



    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>


    Get Preferred Network



    The return is defined in RILConstants.java, e.g. RILConstants.NETWORK_MODE_WCDMA_PREF



    public int getPreferredNetwork() 
    Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
    int preferredNetwork = -1000;
    try
    preferredNetwork = (int) method.invoke(mTelephonyManager);
    Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
    catch (IllegalAccessException e)
    e.printStackTrace();
    catch (InvocationTargetException e)
    e.printStackTrace();


    return preferredNetwork;



    Set Preferred Method.



    The parameter must be based on RILConstants.java, e.g.: RILConstants.NETWORK_MODE_LTE_ONLY



    public void setPreferredNetwork(int networkType) 
    try
    Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
    TelephonyManager.class, new Class int.class);
    Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
    networkType);
    Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
    catch (InvocationTargetException e)
    e.printStackTrace();
    catch (IllegalAccessException e)
    e.printStackTrace();




    This is an utility method to access the hidden API methods.



    /**
    * Get a hidden method instance from a class
    * @param methodName The name of the method to be taken from the class
    * @param fromClass The name of the class that has the method
    * @return A Method instance that can be invoked
    */
    public Method getHiddenMethod(String methodName, Class fromClass, Class params)
    Method method = null;
    try
    Class clazz = Class.forName(fromClass.getName());
    method = clazz.getMethod(methodName, params);
    method.setAccessible(true);
    catch (ClassNotFoundException e)
    e.printStackTrace();
    catch (NoSuchMethodException e)
    e.printStackTrace();


    return method;






    share|improve this answer















    It is possible, I did it.



    For this to work, your app must be signed with the system key or have carrier privilege. Otherwise the app will throw
    java.lang.SecurityException: No modify permission or carrier privilege.



    My app runs on Android 5.1 Lollipop(API 22) and is signed with the system key, so this is the only configuration I can confirm for sure that works. I can't confirm the carrier privilege approach.



    AndroidManifest.xml



    Add this permission to your app manifest. If you are using Android Studio, it will probably mark this line as an error because only system apps can have this permission. If you can sign your app with the system keys, don't worry.



    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>


    Get Preferred Network



    The return is defined in RILConstants.java, e.g. RILConstants.NETWORK_MODE_WCDMA_PREF



    public int getPreferredNetwork() 
    Method method = getHiddenMethod("getPreferredNetworkType", TelephonyManager.class, null);
    int preferredNetwork = -1000;
    try
    preferredNetwork = (int) method.invoke(mTelephonyManager);
    Log.i(TAG, "Preferred Network is ::: " + preferredNetwork);
    catch (IllegalAccessException e)
    e.printStackTrace();
    catch (InvocationTargetException e)
    e.printStackTrace();


    return preferredNetwork;



    Set Preferred Method.



    The parameter must be based on RILConstants.java, e.g.: RILConstants.NETWORK_MODE_LTE_ONLY



    public void setPreferredNetwork(int networkType) 
    try
    Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
    TelephonyManager.class, new Class int.class);
    Boolean success = (Boolean)setPreferredNetwork.invoke(mTelephonyManager,
    networkType);
    Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
    catch (InvocationTargetException e)
    e.printStackTrace();
    catch (IllegalAccessException e)
    e.printStackTrace();




    This is an utility method to access the hidden API methods.



    /**
    * Get a hidden method instance from a class
    * @param methodName The name of the method to be taken from the class
    * @param fromClass The name of the class that has the method
    * @return A Method instance that can be invoked
    */
    public Method getHiddenMethod(String methodName, Class fromClass, Class params)
    Method method = null;
    try
    Class clazz = Class.forName(fromClass.getName());
    method = clazz.getMethod(methodName, params);
    method.setAccessible(true);
    catch (ClassNotFoundException e)
    e.printStackTrace();
    catch (NoSuchMethodException e)
    e.printStackTrace();


    return method;







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 8 '16 at 15:27









    Kozuch

    1,14021432




    1,14021432










    answered Aug 12 '16 at 20:57









    Tulio F.Tulio F.

    9291325




    9291325







    • 1





      Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

      – BamsBamx
      Oct 26 '16 at 10:25











    • Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

      – Tulio F.
      Nov 22 '16 at 17:57











    • This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

      – not2qubit
      Jan 13 '17 at 6:29











    • I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

      – BamsBamx
      Jan 13 '17 at 11:14












    • @BamsBamx And is it on a stock, but rooted ROM?

      – not2qubit
      Jan 13 '17 at 15:03












    • 1





      Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

      – BamsBamx
      Oct 26 '16 at 10:25











    • Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

      – Tulio F.
      Nov 22 '16 at 17:57











    • This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

      – not2qubit
      Jan 13 '17 at 6:29











    • I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

      – BamsBamx
      Jan 13 '17 at 11:14












    • @BamsBamx And is it on a stock, but rooted ROM?

      – not2qubit
      Jan 13 '17 at 15:03







    1




    1





    Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

    – BamsBamx
    Oct 26 '16 at 10:25





    Great solution! Note that I got it working without signing my app (just clicked Run in Android Studio), but moving it into /system/priv-app/<appname>/<appname>.apk and having the MODIFY_PHONE_STATE permission declared in the manifest

    – BamsBamx
    Oct 26 '16 at 10:25













    Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

    – Tulio F.
    Nov 22 '16 at 17:57





    Sure! There are more than one way of doing it. I believe when you run through Android Studio, it installs with more privileges, so it works, been a while now so I don't remember the details.

    – Tulio F.
    Nov 22 '16 at 17:57













    This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

    – not2qubit
    Jan 13 '17 at 6:29





    This is a great answer! Thanks. @BamsBamx are you saying you're running in the AS Device Emulator or on an actual phone?

    – not2qubit
    Jan 13 '17 at 6:29













    I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

    – BamsBamx
    Jan 13 '17 at 11:14






    I did that in a real device, Sony Xperia Z3 Compact running 6.0.1 @user1147688

    – BamsBamx
    Jan 13 '17 at 11:14














    @BamsBamx And is it on a stock, but rooted ROM?

    – not2qubit
    Jan 13 '17 at 15:03





    @BamsBamx And is it on a stock, but rooted ROM?

    – not2qubit
    Jan 13 '17 at 15:03











    1














    This is not an answer, but expanding on Tulio-F's answer.



    The RILConstants.java contain the following:



    // 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)**
    int NETWORK_MODE_CDMA = 4; // CDMA and EvDo (auto mode, according to PRL)**
    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)**
    int NETWORK_MODE_LTE_CDMA_EVDO = 8; // LTE, CDMA and EvDo
    int NETWORK_MODE_LTE_GSM_WCDMA = 9; // LTE, GSM/WCDMA
    int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; // LTE, CDMA, EvDo, GSM/WCDMA
    int NETWORK_MODE_LTE_ONLY = 11; // LTE Only mode.
    int NETWORK_MODE_LTE_WCDMA = 12; // LTE/WCDMA
    int NETWORK_MODE_TDSCDMA_ONLY = 13; // TD-SCDMA only
    int NETWORK_MODE_TDSCDMA_WCDMA = 14; // TD-SCDMA and WCDMA
    int NETWORK_MODE_LTE_TDSCDMA = 15; // TD-SCDMA and LTE
    int NETWORK_MODE_TDSCDMA_GSM = 16; // TD-SCDMA and GSM
    int NETWORK_MODE_LTE_TDSCDMA_GSM = 17; // TD-SCDMA,GSM and LTE
    int NETWORK_MODE_TDSCDMA_GSM_WCDMA = 18; // TD-SCDMA, GSM/WCDMA
    int NETWORK_MODE_LTE_TDSCDMA_WCDMA = 19; // TD-SCDMA, WCDMA and LTE
    int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = 20; // TD-SCDMA, GSM/WCDMA and LTE
    int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 21; // TD-SCDMA,EvDo,CDMA,GSM/WCDMA
    int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; // TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo

    int PREFERRED_NETWORK_MODE = SystemProperties.getInt("ro.telephony.default_network", NETWORK_MODE_WCDMA_PREF);


    Where:



    ** = "AVAILABLE Application Settings menu"






    share|improve this answer



























      1














      This is not an answer, but expanding on Tulio-F's answer.



      The RILConstants.java contain the following:



      // 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)**
      int NETWORK_MODE_CDMA = 4; // CDMA and EvDo (auto mode, according to PRL)**
      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)**
      int NETWORK_MODE_LTE_CDMA_EVDO = 8; // LTE, CDMA and EvDo
      int NETWORK_MODE_LTE_GSM_WCDMA = 9; // LTE, GSM/WCDMA
      int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; // LTE, CDMA, EvDo, GSM/WCDMA
      int NETWORK_MODE_LTE_ONLY = 11; // LTE Only mode.
      int NETWORK_MODE_LTE_WCDMA = 12; // LTE/WCDMA
      int NETWORK_MODE_TDSCDMA_ONLY = 13; // TD-SCDMA only
      int NETWORK_MODE_TDSCDMA_WCDMA = 14; // TD-SCDMA and WCDMA
      int NETWORK_MODE_LTE_TDSCDMA = 15; // TD-SCDMA and LTE
      int NETWORK_MODE_TDSCDMA_GSM = 16; // TD-SCDMA and GSM
      int NETWORK_MODE_LTE_TDSCDMA_GSM = 17; // TD-SCDMA,GSM and LTE
      int NETWORK_MODE_TDSCDMA_GSM_WCDMA = 18; // TD-SCDMA, GSM/WCDMA
      int NETWORK_MODE_LTE_TDSCDMA_WCDMA = 19; // TD-SCDMA, WCDMA and LTE
      int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = 20; // TD-SCDMA, GSM/WCDMA and LTE
      int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 21; // TD-SCDMA,EvDo,CDMA,GSM/WCDMA
      int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; // TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo

      int PREFERRED_NETWORK_MODE = SystemProperties.getInt("ro.telephony.default_network", NETWORK_MODE_WCDMA_PREF);


      Where:



      ** = "AVAILABLE Application Settings menu"






      share|improve this answer

























        1












        1








        1







        This is not an answer, but expanding on Tulio-F's answer.



        The RILConstants.java contain the following:



        // 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)**
        int NETWORK_MODE_CDMA = 4; // CDMA and EvDo (auto mode, according to PRL)**
        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)**
        int NETWORK_MODE_LTE_CDMA_EVDO = 8; // LTE, CDMA and EvDo
        int NETWORK_MODE_LTE_GSM_WCDMA = 9; // LTE, GSM/WCDMA
        int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; // LTE, CDMA, EvDo, GSM/WCDMA
        int NETWORK_MODE_LTE_ONLY = 11; // LTE Only mode.
        int NETWORK_MODE_LTE_WCDMA = 12; // LTE/WCDMA
        int NETWORK_MODE_TDSCDMA_ONLY = 13; // TD-SCDMA only
        int NETWORK_MODE_TDSCDMA_WCDMA = 14; // TD-SCDMA and WCDMA
        int NETWORK_MODE_LTE_TDSCDMA = 15; // TD-SCDMA and LTE
        int NETWORK_MODE_TDSCDMA_GSM = 16; // TD-SCDMA and GSM
        int NETWORK_MODE_LTE_TDSCDMA_GSM = 17; // TD-SCDMA,GSM and LTE
        int NETWORK_MODE_TDSCDMA_GSM_WCDMA = 18; // TD-SCDMA, GSM/WCDMA
        int NETWORK_MODE_LTE_TDSCDMA_WCDMA = 19; // TD-SCDMA, WCDMA and LTE
        int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = 20; // TD-SCDMA, GSM/WCDMA and LTE
        int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 21; // TD-SCDMA,EvDo,CDMA,GSM/WCDMA
        int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; // TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo

        int PREFERRED_NETWORK_MODE = SystemProperties.getInt("ro.telephony.default_network", NETWORK_MODE_WCDMA_PREF);


        Where:



        ** = "AVAILABLE Application Settings menu"






        share|improve this answer













        This is not an answer, but expanding on Tulio-F's answer.



        The RILConstants.java contain the following:



        // 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)**
        int NETWORK_MODE_CDMA = 4; // CDMA and EvDo (auto mode, according to PRL)**
        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)**
        int NETWORK_MODE_LTE_CDMA_EVDO = 8; // LTE, CDMA and EvDo
        int NETWORK_MODE_LTE_GSM_WCDMA = 9; // LTE, GSM/WCDMA
        int NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA = 10; // LTE, CDMA, EvDo, GSM/WCDMA
        int NETWORK_MODE_LTE_ONLY = 11; // LTE Only mode.
        int NETWORK_MODE_LTE_WCDMA = 12; // LTE/WCDMA
        int NETWORK_MODE_TDSCDMA_ONLY = 13; // TD-SCDMA only
        int NETWORK_MODE_TDSCDMA_WCDMA = 14; // TD-SCDMA and WCDMA
        int NETWORK_MODE_LTE_TDSCDMA = 15; // TD-SCDMA and LTE
        int NETWORK_MODE_TDSCDMA_GSM = 16; // TD-SCDMA and GSM
        int NETWORK_MODE_LTE_TDSCDMA_GSM = 17; // TD-SCDMA,GSM and LTE
        int NETWORK_MODE_TDSCDMA_GSM_WCDMA = 18; // TD-SCDMA, GSM/WCDMA
        int NETWORK_MODE_LTE_TDSCDMA_WCDMA = 19; // TD-SCDMA, WCDMA and LTE
        int NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA = 20; // TD-SCDMA, GSM/WCDMA and LTE
        int NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 21; // TD-SCDMA,EvDo,CDMA,GSM/WCDMA
        int NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA = 22; // TD-SCDMA/LTE/GSM/WCDMA, CDMA, and EvDo

        int PREFERRED_NETWORK_MODE = SystemProperties.getInt("ro.telephony.default_network", NETWORK_MODE_WCDMA_PREF);


        Where:



        ** = "AVAILABLE Application Settings menu"







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 13 '17 at 6:42









        not2qubitnot2qubit

        4,25813762




        4,25813762



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10170179%2fchange-mobile-network-mode-gsm-wcdma-auto%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo