Android Mobile, Gps and Wifi settings
<p>I want to share my Settings Page in Android. It allows to turn on/off gps, wifi and mobile data. I looked for this sollution a long time, and now, when I finally wrote it, maybe this code will be helpful to many of you.</p>

<p>If you want to correct some of my sollutions go ahead.</p>

<pre><code>public class Settings extends Activity {
    ToggleButton        wifi, network, gps;

    WifiManager         wifiManager;
    ConnectivityManager connectivityManager;
    LocationManager     locationManager;

    Intent              gps_settings;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    gps_settings = new Intent("android.location.GPS_ENABLED_CHANGE");

    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    wifi = (ToggleButton) findViewById(R.id.toggleWiFi);
    network = (ToggleButton) findViewById(R.id.toggleGSM);
    gps = (ToggleButton) findViewById(R.id.toggleGPS);

    wifi.setOnClickListener(new OnClickListener() {
        String  provider    = "Wifi";

        public void onClick(View v) {
            if (!wifi.isChecked()) wifiManager.setWifiEnabled(makeToast(provider, false));
            else if (!wifiManager.isWifiEnabled()) wifiManager.setWifiEnabled(makeToast(provider, true));
        }
    });

    network.setOnClickListener(new OnClickListener() {
        String  provider    = "Mobile data";

        public void onClick(View v) {
            try {
                if (!network.isChecked()) setMobileDataEnabled(makeToast(provider, false));
                else if (!connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting()) setMobileDataEnabled(makeToast(provider, true));
            }
            catch (Exception e) {
                Log.e("Debugging", "Reflexion error.");
            }
        }
    });

    gps.setOnClickListener(new OnClickListener() {
        String  provider    = "GPS";

        public void onClick(View v) {
            if (!gps.isChecked()) {
                gps_settings.putExtra("enabled", makeToast(provider, false));
                sendBroadcast(gps_settings);
            }

            else if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                gps_settings.putExtra("enabled", makeToast(provider, true));
                sendBroadcast(gps_settings);
            }
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    toggle();
}

protected void toggle() {
    if (wifiManager.isWifiEnabled()) wifi.setChecked(true);
    else wifi.setChecked(false);

    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting()) network.setChecked(true);
    else network.setChecked(false);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) gps.setChecked(true);
    else gps.setChecked(false);
}

protected void setMobileDataEnabled(boolean enabled) throws SecurityException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    final Class&lt;?&gt; conmanClass = Class.forName(connectivityManager.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(connectivityManager);
    final Class&lt;?&gt; iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

protected boolean makeToast(String provider, boolean turningOn) {
    Toast.makeText(getApplicationContext(), "Turning "
            + (turningOn ? "on" : "off") + " " + provider + "...", Toast.LENGTH_SHORT).show();

    return turningOn;
    }
}
</code></pre>

<hr>

<p><strong>Edited</strong></p>

<p>Are all of these methods safe? Do I use them correctly? Is there anything to do in better way?</p>
