1

I have a ToggleButton named gprs. I need it to turn on and off the gprs. How to accomplish that? I've looked here but it gives errros and I can't figure it out how to use it in my case.

Community
  • 1
  • 1
Nabukodonosor
  • 699
  • 1
  • 8
  • 20

1 Answers1

0

OK, I will post here the solution if anyone has the same problem, using toggle button. First I created separated class for gprs settings:

public class GprsSettings {

    static 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);
            Log.i("setMobileDataEnabled()","OK");
        } 

        catch (Exception e) 
        {
            e.printStackTrace();
            Log.i("setMobileDataEnabled()","FAIL");
        }         
    }
}

Then, first in my activity add some code to check if the gprs is on or off....place it above your onCreate method:

private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        // There are no active networks.
        return false;
    } else
        return true;
    }
}

Then, in my activity I used this code for toggle button with toast:

gprs.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        try {
            if (((ToggleButton)v).isChecked()) {
                GprsSettings.setMobileDataEnabled(getApplicationContext(), true);
                Toast.makeText(getApplicationContext(), "GPRS is ON", Toast.LENGTH_LONG).show();
            }else{    
                GprsSettings.setMobileDataEnabled(getApplicationContext(), false);
                Toast.makeText(getApplicationContext(), "GPRS is OFF", Toast.LENGTH_LONG).show();
            }
        }
        catch (Exception localException) {
            Log.e("SwarmPopup", "error on GPRS listerner: " + localException.getMessage(), localException);
        }
    }
});
gprs.setChecked(isNetworkConnected());

That's it, works like a charm.

sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Nabukodonosor
  • 699
  • 1
  • 8
  • 20
  • Actually it does not work like a charm. It works, but...the gprs button is on even if gprs on my phone is off but wireless is on. :(( – Nabukodonosor Mar 13 '13 at 16:22