ACTION_BATTERY_LOW not fired from manifest?

Does action_battery_low firing from the manifest because I thought it was?

Here is my manifest for him:

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

But it never works when I get a low battery warning from the system. Can it only be open?

+2
source share
5 answers

The original question says that the recipient does not receive intentions. This is because the receiver was declared as <reciever> , not <receiver> . If the receiver element were declared correctly, this would work.

Another important source of confusion is the fact that the Android documentation incorrectly references "android.intent.action.ACTION_BATTERY_LOW" and "android.intent.action.ACTION_BATTERY_OKAY" . There is an existing Android issue for this documentation error, but you should be aware that some of the comments on this issue are misleading.

Instead, the receiver actions should be "android.intent.action.BATTERY_LOW" and "android.intent.action.BATTERY_OKAY" . When referencing these actions from a Java source, you can use the android.content.Intent.ACTION_BATTERY_LOW and android.content.Intent.ACTION_BATTERY_OKAY constants, which are defined correctly.

Unfortunately, Reto Meier also incorrectly defines the action in Deep Dive Into Location . A question was also asked for this.

+15
source

From my own testing, I ran into the same problem and solution as GeoBio Boo

But then I examined my code in more detail and noticed that I was filtering the android.intent.action.ACTION_BATTERY_LOW action, as described in the docs: https://developer.android.com/training/monitoring-device-state/battery-monitoring. html

In fact, the action isroid.intent.action.BATTERY_LOW (no ACTION_). Once I made this change, I was able to register the receiver in the manifest and successfully accept the event (verified using the emulator power command)

+2
source

Do not take into account the offer of permission in this answer, it is incorrect.

You may have permission to request to catch the BATTERY_LOW action. Try adding <uses-permission android:name="android.permission.BATTERY_STATS"/> to your manifest.

In addition, you can put several actions in the same intent filter, for example:

 <reciever android:name=".BatteryReciever"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BATTERY_LOW"/> </intent-filter> </reciever> 
+1
source

You need to program the program for ACTION_BATTERY_LOW . I tried to understand this for a very long time and realized that registering it in AndroidManifest.xml does not work.

In other words, in a call to onResume () or onCreate (): registerReceiver (receiver, new IntentFilter (Intent.ACTION_BATTERY_LOW));

You do not need BATTERY_STATS permission.

+1
source

your manifest call is correct, how about your receiver?

according to Reto Meyer in his legendary Deep Dive Into Location , you should use:

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

and your recipient activity should be checked

 boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW); 

I took one step and listened to 5 events related to the battery:

  <receiver android:name=".ReceiverBatteryLevel"> <intent-filter> <action android:name="android.intent.action.ACTION_BATTERY_LOW"/> <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> <action android:name="android.intent.action.ACTION_BATTERY_CHANGED"/> </intent-filter> </receiver> 

and then get them like this (abbreviated, fill in at the end)

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.BatteryManager; import android.util.Log; public class ReceiverBatteryLevel extends BroadcastReceiver { private final String TAG = "TGbattery"; int scale = -1; int level = -1; int voltage = -1; int temp = -1; public void onReceive(Context context, Intent intent) { Log.d(TAG,"battery Receiver was called now"); String deviceUuid = "INVALID_IMEI"; boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW); boolean batteryOK = intent.getAction().equals(Intent.ACTION_BATTERY_OKAY); boolean batteryPowerOn = intent.getAction().equals(Intent.ACTION_POWER_CONNECTED); boolean batteryPowerOff = intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED); boolean batteryChange = intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED); String intentAction = intent.getAction(); // register SHUTDOWN event try { 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); Log.d(TAG,intentAction+" batteryChange="+batteryChange+" flagLo="+batteryLow+" batteryOK="+batteryOK+" batteryPowerOn="+batteryPowerOn+" batteryPowerOff="+batteryPowerOff+"\n level="+level+" temp="+temp+" scale="+scale+" voltage="+voltage); } // catch etc } } 

I must admit that I do not like the results of BatteryManager. Any criticism is welcome.

-1
source

All Articles