0

Android で自分の設定ページを共有したいと考えています。GPS、Wi-Fi、モバイルデータのオン/オフを切り替えることができます。私は長い間この解決策を探していましたが、ついにそれを書いたとき、このコードは多くの人にとって役立つかもしれません.

私のソリューションのいくつかを修正したい場合は、先に進んでください。

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<?> conmanClass = Class.forName(connectivityManager.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(connectivityManager);
    final Class<?> 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;
    }
}

編集済み

これらの方法はすべて安全ですか? それらを正しく使用していますか?もっと良い方法はありますか?

4

0 に答える 0