You must disable mobile data transfer programmatically. The slider itself is located in the Settings - Data transfer - Mobile data transfer menu. I tried to turn off and turn on Wi-Fi, it's simple:

wifi = (WifiManager)getSystemService(service); if (!wifi.isWifiEnabled()){ if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) wifi.setWifiEnabled(true); } else{ wifi.setWifiEnabled(false); } 

but this is a problem.

  • one
    Again, what if I answer? - Barmaley
  • anymore :) - andreich

1 answer 1

Try it:

 void turnData(boolean ON) throws Exception { if(Build.VERSION.SDK_INT == Build.VERSION_CODES.FROYO) { Log.i("version:", "Found Froyo"); try{ Method dataConnSwitchmethod; Class telephonyManagerClass; Object ITelephonyStub; Class ITelephonyClass; TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().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()); if (ON) { dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); } else { dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity"); } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub); }catch(Exception e){ Log.e("Error:",e.toString()); } } else { Log.i("version:", "Found Gingerbread+"); final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().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, ON); } } 
  • one
    Yeah, I see copy-paste from the stack ... Well, in any case, this is the answer. - Barmaley