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!

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Adarsh H S
  • 1,208
  • 6
  • 21
  • 42

5 Answers5

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

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
k3v
  • 1,189
  • 12
  • 12
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);
Rahul
  • 3,293
  • 2
  • 31
  • 43
Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • Thanks for your reply. I tried registerReceiver(myBatteryReceiverLow , new IntentFilter( Intent.ACTION_BATTERY_LOW)); But its not working too :( – Adarsh H S Nov 05 '12 at 09:23
  • 1
    Have you cleaned your manifest? And send broadcast action handly ACTION_BATTERY_LOW = "android.intent.action.BATTERY_LOW"; – Yahor10 Nov 05 '12 at 09:35
  • 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? – Adarsh H S Nov 05 '12 at 09:45
  • Read this questions : http://stackoverflow.com/questions/3661464/get-battery-level-before-broadcast-receiver-responds-for-intent-action-battery-c , http://stackoverflow.com/questions/2584497/what-is-the-difference-between-sendstickybroadcast-and-sendbroadcast-in-android – Yahor10 Nov 05 '12 at 09:51
  • 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! – Adarsh H S Nov 05 '12 at 10:04
  • the question is related to when battery gets low, but the example is on changed, irrelevant example. – Wang'l Pakhrin Jun 05 '16 at 07:20
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 :-)

Rahul
  • 3,293
  • 2
  • 31
  • 43
EZDsIt
  • 921
  • 1
  • 9
  • 14
  • Well the new document also mentions ACTION_BATTERY_LOW – Aman Alam May 28 '16 at 12:22
  • Yes, K3v's answer is absolutely correct, that should be the accepted answer – Aman Alam May 28 '16 at 12:30
  • 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 work – Aman Alam May 28 '16 at 12:56
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

chris
  • 194
  • 2
  • 10
-1

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

A_Sk
  • 4,532
  • 3
  • 27
  • 51
seb
  • 9