2

i want to make widget for home screen through which i can enable / disable packet data of network provider.

I know how to make widget but if someone will help me to enable / disable packet data through coding then it will great help

thanks in advance

rajpara
  • 5,203
  • 1
  • 29
  • 46
  • 1
    Just to point you in a general direction, there is a programatic way to do it in 2.3 and up, 2.2 and before you have to do it by changing the APN Name. – blindstuff Jan 09 '12 at 13:59
  • Have a look at https://stackoverflow.com/questions/3644144/how-to-disable-mobile-data-on-android. The author of the accepted answer achieved this by accessing the ITelephony interface via java reflection. – Andre Jan 09 '12 at 13:32

1 Answers1

1
private void setMobileDataEnabled(Context context, boolean enabled) {
    try
    {
        final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
    }
    catch (Exception e) 
    {
        // TODO: handle exception
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Neeraj Sharma
  • 463
  • 1
  • 9
  • 27