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

5

14

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!

Mord answered 5/11, 2012 at 9:2 Comment(0)
A
7

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);
Addendum answered 5/11, 2012 at 9:7 Comment(6)
Thanks for your reply. I tried registerReceiver(myBatteryReceiverLow , new IntentFilter( Intent.ACTION_BATTERY_LOW)); But its not working too :(Mord
Have you cleaned your manifest? And send broadcast action handly ACTION_BATTERY_LOW = "android.intent.action.BATTERY_LOW";Addendum
I have cleaned manifest. But what does send broadcast action handly mean? I haven't worked much on broadcasts.. Can you please help me out?Mord
Read this questions : #3661964 , #2584997Addendum
I have gone through those questions. Not helping much! :( Also ACTION_BATTERY_CHANGED is a sticky broadcast which cannot be received through components declared in manifests, but only by explicitly registering for it with Context.registerReceiver() But where as ACTION_BATTERY_LOW is not sticky broadcast!Mord
the question is related to when battery gets low, but the example is on changed, irrelevant example.Aeronaut
G
41

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).

Glutamate answered 14/5, 2013 at 19:20 Comment(0)
A
7

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);
Addendum answered 5/11, 2012 at 9:7 Comment(6)
Thanks for your reply. I tried registerReceiver(myBatteryReceiverLow , new IntentFilter( Intent.ACTION_BATTERY_LOW)); But its not working too :(Mord
Have you cleaned your manifest? And send broadcast action handly ACTION_BATTERY_LOW = "android.intent.action.BATTERY_LOW";Addendum
I have cleaned manifest. But what does send broadcast action handly mean? I haven't worked much on broadcasts.. Can you please help me out?Mord
Read this questions : #3661964 , #2584997Addendum
I have gone through those questions. Not helping much! :( Also ACTION_BATTERY_CHANGED is a sticky broadcast which cannot be received through components declared in manifests, but only by explicitly registering for it with Context.registerReceiver() But where as ACTION_BATTERY_LOW is not sticky broadcast!Mord
the question is related to when battery gets low, but the example is on changed, irrelevant example.Aeronaut
B
7

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 :-)

Billowy answered 22/8, 2013 at 21:29 Comment(3)
Well the new document also mentions ACTION_BATTERY_LOWEarnestineearnings
Yes, K3v's answer is absolutely correct, that should be the accepted answerEarnestineearnings
Just had a discussion with CommonsWare on another question. He rightly points out that with BATTERY_LOW, we start referring the string value of the ACTION_BATTERY_LOW constant. Will need to figure out why the constant doesn't workEarnestineearnings
F
1

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

Freewheeling answered 12/2, 2019 at 19:45 Comment(0)
S
-1

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

Summersummerhouse answered 23/9, 2015 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.