|
8 | 8 | import android.content.SharedPreferences; |
9 | 9 | import android.hardware.camera2.CameraManager; |
10 | 10 | import android.net.Uri; |
| 11 | +import android.net.wifi.WifiManager; |
11 | 12 | import android.os.Build; |
12 | 13 | import android.os.Bundle; |
13 | 14 | import android.os.Handler; |
@@ -49,6 +50,16 @@ public static void launchWriteSettings(Context context) { |
49 | 50 | context.startActivity(i); |
50 | 51 | } |
51 | 52 |
|
| 53 | + public static void toggleWifi(Context context) { |
| 54 | + WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); |
| 55 | + boolean stateToSet = !wifiManager.isWifiEnabled(); |
| 56 | + if(!wifiManager.setWifiEnabled(stateToSet)) { |
| 57 | + // try with root |
| 58 | + toggleDataWifiBluetoothGps(context, "wifi", Misc::isWifiEnabled); |
| 59 | + } |
| 60 | + else Toast.makeText(context, "Wifi "+(stateToSet?"enabled":"disabled"), Toast.LENGTH_SHORT).show(); |
| 61 | + } |
| 62 | + |
52 | 63 | public static void toggleDataWifiBluetoothGps(Context context, String channel, II ii) { // channel: "data" or "wifi" |
53 | 64 | String[][] cmdsAndErrors = { |
54 | 65 | {channel + " currently DISABLED -> enabling...", "enable"}, |
@@ -275,7 +286,7 @@ public void toggle(View v) { |
275 | 286 | toggleDataWifiBluetoothGps(this, "data", Misc::isDataConnectionEnabled); |
276 | 287 | break; |
277 | 288 | case R.id.toggleWifi: |
278 | | - toggleDataWifiBluetoothGps(this, "wifi", Misc::isWifiEnabled); |
| 289 | + toggleWifi(this); |
279 | 290 | break; |
280 | 291 | case R.id.toggleBt: |
281 | 292 | // toggleDataWifiBluetoothGps(this, "bluetooth", Misc::isBluetoothEnabled); // no need to do this using root |
|
0 commit comments