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();
I must admit that I do not like the results of BatteryManager. Any criticism is welcome.
source share