0

我想在 Android 中分享我的设置页面。它允许打开/关闭 gps、wifi 和移动数据。我寻找这个解决方案很长时间,现在,当我终于写出来时,也许这段代码会对你们中的许多人有所帮助。

如果您想纠正我的一些解决方案,请继续。

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