Search code examples
androidandroid-wifitogglebuttonbatterylevel

How can i disable wifi when battery level is less 10%?


I'm writing an application that when the battery level is less of 10% the wifi connection (if activate) will disable. This is what i want but not what i am able to do :). I've created a toggle and when pressed the wifi stops and vice versa if press again the wifi turns on in this way:

    public void getRisparmio(View view) {
    // is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    WifiManager wifiManager;
    if (on) {
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(false);
    } else {
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(tru);
    }
    }

in this way i get the battery level:

int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);

what i want is something like

    public void getRisparmio(View view) {
    // is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    WifiManager wifiManager;
    if (on) {
    if (level < 10){
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(false);
    }
    } else {
      wifiManager(WifiManager) this.getSystemService(Context.WIFI_SERVICE);
      wifiManager.setWifiEnabled(true);
    }
    }

Is it possible? Thanks


Solution

  • If you want to check battery level when you click on button:

    public void getRisparmio(View view) {
    
       .....
       IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
       Intent batteryStatus = context.registerReceiver(null, ifilter);
       int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
       if (level<10){
           ......
       }
       .......
    }
    

    If you want to disable wifi when battery level is low,you can register a Broadcast receiver with with this IntentFilter Intent.ACTION_BATTERY_LOW.

    public class BatteryReceiver extends BroadcastReceiver {
    
        public void onReceive(Context context, Intent intent) {
    
               boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
               if (batteryLow){
                      ....
                       level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                       if (level<xx) {
                         //call your code
                         //wifiManager.setWifiEnabled(false);
                       }
               }
        }
    }
    

    In your Activity in onResume() you can register your broadcast receiver

    BatteryReceiver receiver = new BatteryReceiver();
    IntentFilter inf = new IntentFilter();
    inf.addAction(BatteryReceiver); 
    registerReceiver(receiver, inf);
    

    Remember to unregister receiver in onPause().

    Otherwise you can register in Manifest.xml

    <reciever android:name=".BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BATTERY_LOW"/>
        </intent-filter>
    </reciever>