0

I have to enable/disable data in the same usecase, that i used code given here How to disable Mobile Data on Android

private void setMobileDataEnabled(Context context, boolean enabled) {
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);

}

but problem is that when i am passing false it is disconnecting with adb. can we disable data without disconnecting adb

Adding logcat when passing false

12-10 12:18:20.261: D/ConnectivityService(150): setMobileDataEnabled(false)
12-10 12:18:20.321: W/BackupManagerService(150): dataChanged but no participant             pkg='com.android.providers.settings' uid=1001
12-10 12:18:20.681: D/ConnectivityService(150): ConnectivityChange for mobile: DISCONNECTED/DISCONNECTED
12-10 12:18:20.691: D/ConnectivityService(150): Attempting to switch to wifi
12-10 12:18:20.811: D/dalvikvm(233): GC_CONCURRENT freed 505K, 7% free 8848K/9479K, paused 91ms+112ms, total 443ms
12-10 12:18:20.821: D/ConnectivityService(150): resetConnections(eth0, 3)
12-10 12:18:20.821: D/NetUtils(150): android_net_utils_resetConnections in  env=0x2a13bfc8 clazz=0x7be00001 iface=eth0 mask=0x3
: E/(): Device disconnected
Community
  • 1
  • 1
ashishdd
  • 23
  • 4
  • 1
    Disabling mobile data this way will not have any effect on the adb connection. If your adb connection to your app is disconnected, your app is probably crashing. Check your logcat. – David Wasser Dec 05 '13 at 09:17
  • @DavidWasser , added the logcat for passing false. can you provide some useful information - thanks – ashishdd Dec 10 '13 at 06:54
  • It is happening only on emulator. – ashishdd Dec 10 '13 at 08:45
  • Sorry, I've never tried to do this on an emulator. I wouldn't trust the behaviour on an emulator anyway as the telephony code can be customized by device manufacturers. – David Wasser Dec 10 '13 at 18:53

1 Answers1

0
public void onClick(View view){
        ConnectivityManager dataManager;
        dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        Method dataMtd = null;
        try {
            dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dataMtd.setAccessible(true);
        try {
            dataMtd.invoke(dataManager, true);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
Hitman
  • 588
  • 4
  • 10