2

I want to register broadcast for low battery. If battery status reached at some level I want to get alert...

Please if you have any idea please help me..

Dharmendra
  • 33,296
  • 22
  • 86
  • 129

2 Answers2

5

You will need to register a BroadcastReceiver for ACTION_BATTERY_LOW. You can do this in the manifest with a <receiver> element or via registerReceiver() from some already-running component of your application.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • As per this answer : http://stackoverflow.com/a/16551151/243709, the documentation is incorrect on this; and instead of ACTION_BATTERY_LOW, using BATTERY_LOW will work, similarly for ACTION_BATTERY_OKAY too – Aman Alam May 28 '16 at 12:31
  • @SheikhAman: The documentation that you linked to is correct. – CommonsWare May 28 '16 at 12:32
  • I am right now coding a small app to detect critical low level of batteries, and ACTION_BATTERY_LOW, as said in the documentation, doesn't work. BATTERY_LOW worked, but doesn't seem to be documented. Or might be an Android version related thing too – Aman Alam May 28 '16 at 12:47
  • @SheikhAman: "BATTERY_LOW worked, but doesn't seem to be documented" -- [`ACTION_BATTERY_LOW`](https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_LOW) is a Java `static final` field. It points to a string. That string is `android.intent.action.BATTERY_LOW`, as is mentioned in [the documentation](https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_LOW). It works this way for most of the `ACTION_` values -- for `ACTION_FOO`, the string is `android.intent.action.FOO`. – CommonsWare May 28 '16 at 12:51
  • Got it, so I am using the constant value (not preferred), which works, but it's constant doesn't. Weird, Will dig more about why. Thanks! – Aman Alam May 28 '16 at 12:55
  • Or maybe I need some sleep ;-) – Aman Alam May 28 '16 at 12:58
4

For Battery_Status_Low, You could try registering it through a Service for example, like:

BatteryReceiver receiver = new BatteryReceiver(); 
IntentFilter inf = new IntentFilter();
inf.addAction("android.intent.action.BATTERY_LOW");`
registerReceiver(receiver, inf);
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104