简体   繁体   中英

How to detect when the Battery's low : Android?

I want to close my app when the battery level of the device gets low. I have added following codes in manifest.

 <receiver android:name=".BatteryLevelReceiver" 
         <intent-filter>
            <action android:name="android.intent.action.ACTION_BATTERY_LOW" />
            <action android:name="android.intent.action.ACTION_BATTERY_OKAY" />
        </intent-filter>
 </receiver>

And following code in receiver

public class BatteryLevelReceiver extends BroadcastReceiver 
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Toast.makeText(context, "BAttery's dying!!", Toast.LENGTH_LONG).show();
        Log.e("", "BATTERY LOW!!");
    }
}

I am running the app on emulater and changing the battery level using telnet. It changes the battery level but not showing any toast or logs.

What am I missing?? Any help is appreciated! Thank you.

You can register your receiver in the AndroidManifest.xml , however make sure that the action you are filtering on is

android.intent.action.BATTERY_LOW

and not

android.intent.action.ACTION_BATTERY_LOW

(which you have used in your code).

Register your receiver in the code, not in the AndroidManifest file.

registerReceiver(batteryChangeReceiver, new IntentFilter(
    Intent.ACTION_BATTERY_CHANGED)); // register in activity or service

public class BatteryChangeReceiver extends BroadcastReceiver {

    int scale = -1;
    int level = -1;
    int voltage = -1;
    int temp = -1;

    @Override
    public void onReceive(Context context, Intent intent) {
        level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
        voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
    }
}

unregisterReceiver(batteryChangeReceiver);//unregister in the activity or service

Or listen to the battery level with null receiver.

Intent BATTERYintent = this.registerReceiver(null, new IntentFilter(
        Intent.ACTION_BATTERY_CHANGED));
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
Log.v(null, "LEVEL" + level);

k3v is correct.

There is actually an error in the documentation. It specifically says to use android.intent.action.ACTION_BATTERY_LOW . But the correct action to put in the manifest is android.intent.action.BATTERY_LOW See here: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

(Couldn't vote k3v's answer up, not enough StackOverflow point things...)

UPDATE: I now can and did up-vote k3v's answer :-)

Register using context register . If you are targeting Android 8.0 or higher, you cannot use manifest declared receiver. Paste this code in your main activity to register.

 BroadcastReceiver receiver = new BatteryLevelReceiver();
        IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
        filter.addAction(Intent.ACTION_BATTERY_LOW);
        this.registerReceiver(receiver, filter);

and you are good to go PS the emulator should not be in charging state

Maybe the emulator does not respond to BATTERY_LOW and BATTERY_OKAY messages. Try on a real Android device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM