androidandroid-2.2-froyo3g-network

Enable/Disable Mobile Data (GPRS) using code on Froyo 2.2


I'm trying to Enable / Disable Mobile Data Connexion. I've used this code by rIHaN JiTHiN (Enable/Disable Mobile Data (GPRS) using code) and it's works perfectly on Android 4.0, but it's doesn't on my Galaxy S (Froyo 2.2)...

Is there a way to enable / disable data connexion programmatically ?

If anyone had any idea why it's doesn't work on Froyo, would be really helpful. According to rIHaN JiTHiN, this code works on all Android version...


Solution

  • You can check whether it is enabled or disabled by using below code

    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
    if (mMobile.isConnected()) {
        //if internet connected
    }
    

    if it is disabled, you can enable it on your froyo device by using this one

    void turnData(boolean ON) throws Exception
    {
    
    if(bv == 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);
    }
    

    and also dont forget to add these to manifest

    android.permission.UPDATE_DEVICE_STATS
    android.permission.CHANGE_NETWORK_STATE
    android.permission.ACCESS_NETWORK_STATE
    android.permission.MODIFY_PHONE_STATE
    android.permission.READ_PHONE_STATE