Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/disable data connection in android programmatically

I want to enable/disable the data connection programmatically. I've used the following code:

void enableInternet(boolean yes) {     ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);     Method iMthd = null;     try {         iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);         } catch (Exception e) {                }      iMthd.setAccessible(false);      if(yes)      {                  try {                     iMthd.invoke(iMgr, true);                     Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();                 } catch (IllegalArgumentException e) {                     // TODO Auto-generated catch block                      dataButton.setChecked(false);                      Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();                  } catch (IllegalAccessException e) {                     // TODO Auto-generated catch block                     Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();                      e.printStackTrace();                 } catch (InvocationTargetException e) {                     // TODO Auto-generated catch block                      dataButton.setChecked(false);                      Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();                  }       }     else      {         try {             iMthd.invoke(iMgr, true);             Toast.makeText(getApplicationContext(), "Data connection Disabled", Toast.LENGTH_SHORT).show();             } catch (Exception e) {                    dataButton.setChecked(true);                 Toast.makeText(getApplicationContext(), "Error Disabling Data connection", Toast.LENGTH_SHORT).show();                                     }      } } 

It's working without any errors in the emulator but, I'm getting "InvocationTargetException" when I try to run it on a real device. I'm using API level 8 to build the application.

like image 925
JiTHiN Avatar asked Jul 19 '12 06:07

JiTHiN


People also ask

What is enable and disable mobile data in Android programmatically?

For your find Information, unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps. setMobileDataEnabled() method is no longer callable via reflection.

Can you turn off the data connection?

Swipe down from the top of the screen, select Settings, press Data usage and then flick the Mobile data switch from On to Off – this will completely turn off your mobile data connection. Note: you will still be able to connect to the internet and use apps as normal if you are connected to a Wi-Fi network.


1 Answers

This code sample should work for android phones running gingerbread and higher:

private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {     final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);     final Class conmanClass = Class.forName(conman.getClass().getName());     final Field connectivityManagerField = conmanClass.getDeclaredField("mService");     connectivityManagerField.setAccessible(true);     final Object connectivityManager = connectivityManagerField.get(conman);     final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());     final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);     setMobileDataEnabledMethod.setAccessible(true);      setMobileDataEnabledMethod.invoke(connectivityManager, enabled); } 

Dont forget to add this line to your manifest file

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
like image 200
Tobias Moe Thorstensen Avatar answered Sep 23 '22 01:09

Tobias Moe Thorstensen