Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED
Is there a way to nudge this to get it working or simply run some code to see what the battery level was on
the last broadcast?
You can call registerReceiver()
with your IntentFilter
and a null
BroadcastReceiver
to get the last-broadcast Intent
. This works because ACTION_BATTERY_CHANGED
is a so-called "sticky broadcast", which I describe a bit more in this StackOverflow question-and-answer.
Get Battery Level with BroadcastReceiver in Android Service
take a look at this, it will get you the battery level you need so you can start your service. Just use BatteryManager.EXTRA_LEVEL
as the key to get the level from the intent
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
android.intent.action.BATTERY_CHANGED only when battery level changes not when registering receiver
Nope, there's no real knobs on it. You're registered or you're not. What you can do is set a static variable in your receiver that's defaulted to false. In your onReceive, look at that variable. If it's false, set it to true and return (do nothing else). It its true, process the event. It needs to be a static because receivers are not necessarily the same object between calls if launched via the manifest (haven't checked when registered via registerReceiver).
Android :How to get Battery status without Broadcast Receiver for API level 17 and above?
Intent battery=registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
battery
will contain the last-broadcast ACTION_BATTERY_CHANGED
Intent
, and its extras will contain the battery information, using the extra keys documented on BatteryManager
.
(this code will work inside a method of a Context
subclass; alternatively, call registerReceiver()
on some other Context
)
Get battery level only once using Android SDK
The Intent.ACTION_BATTERY_CHANGED
broadcast is what's known as a "sticky broadcast." Because this is sticky, you can register for the broadcast with a null receiver which will only get the battery level one time when you call registerReceiver
.
A function to get the battery level without receiving updates would look something like this:
public float getBatteryLevel() {
Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// Error checking that probably isn't needed but I added just in case.
if(level == -1 || scale == -1) {
return 50.0f;
}
return ((float)level / (float)scale) * 100.0f;
}
More data can be pulled from this sticky broadcast. Using the returned batteryIntent
you can access other extras as outlined in the BatteryManager
class.
getting BATTERY_CHANGED flg=0x60000010 (has extras) when trying to do something when battery is at a defined level
I figured it out after reading this post
https://stackoverflow.com/a/23791368/9308420
so, instead of
_textView2.setText(percent);
Changed to
_textView2.setText("" + percent);
Related Topics
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
Error: Configuration with Name 'Default' Not Found in Android Studio
Sharedpreferences and Thread Safety
Event Onclick for a Button in a Custom Notification
How to Open a Fragment on Button Click from a Fragment in Android
How to Change the Font Size of Tabhost in Android
Exporting SQLite Database to CSV File in Android
Authentication Using Facebook at First and Then Google Causes an Error in Firebase for Android
How to Change Text Color of Simple List Item
Audiotrack, Soundpool or Mediaplayer Which Should I Use
Most Memory Efficient Way to Resize Bitmaps on Android
View the Task's Activity Stack
What Is the Relationship Between Looper, Handler and Messagequeue in Android
How Can a Divider Line Be Added in an Android Recyclerview
Full Text Search Example in Android