0

In my Application I want to do some action below battery level like 30%, 20% and 10%. The user can select any of the above battery level to perform the operation.

My question is, how can I determine the battery level is 30% or 20% or 10% to perform my work?

I have register a battery broadcast receiver in manifest file as below.

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

But I don't know when will (on what battery level) I receive it in my receiver class? or in other way when will the ACTION_BATTERY_LOW intent get fired (at what battery level)?

Please help!

MCA Shah
  • 405
  • 1
  • 4
  • 10
  • possible duplicate of [When android fires ACTION\_BATTERY\_LOW](http://stackoverflow.com/questions/11970185/when-android-fires-action-battery-low) – Shereef Marzouk May 12 '14 at 06:21

2 Answers2

0

Please check this answer

This part of the answer above should answer your question:

mLowBatteryWarningLevel = mContext.getResources().getInteger(
            com.android.internal.R.integer.config_lowBatteryWarningLevel);

it will tell you this device's specific threshold but you cannot change it!

Community
  • 1
  • 1
Shereef Marzouk
  • 3,282
  • 7
  • 41
  • 64
  • Who ever voted me down, would you mind telling me why ? – Shereef Marzouk May 12 '14 at 05:57
  • 2
    I voted you down and flagged the post, because you simply copied/plagiarized someone else's accepted answer. That's not how Stackoverflow works. If it's a duplicate question, then flag it as such. FWIW, your plagiarized answer doesn't actually answer the question. – 323go May 12 '14 at 05:58
  • Thank you @323go, but Is it ok now ?? P.S. I only copied it until i could rephrase it in my edit because i need to answer before others – Shereef Marzouk May 12 '14 at 05:59
  • "because i need to answer before others" - no you don't. You need to answer correctly. This isn't about getting points, but about getting correct information to those who need it. Adding an incorrect answer just so you're first is confusing to the reader. Still -- your answer doesn't actually provide a solution to the asker. – 323go May 12 '14 at 06:03
  • I am not gonna get in an argument here, if you know a link to a relevant chat here on stackoverflow we can go there. but anyways I think he knows how to get the current level and he needed to know when does the system usually calls his broadcast receiver, anyways Thanks for commenting and answering him, we are all trying to help but i am also trying to get points while helping, I bow to you sir! Also @323go please read http://meta.stackexchange.com/q/17204 – Shereef Marzouk May 12 '14 at 06:08
  • http://stackoverflow.com/help/duplicates -- if you can reuse an answer to another question, then the question is a duplicate. You could have flagged it as such, and pointed the asker in the right direction. Of course, there are no points for comments -- but again, the reputation points shouldn't be your #1 motivation here, or you're in the wrong spot. Further, google "Stackoverflow plagiarism" and you'll see that it's an often discussed issue on Meta. – 323go May 12 '14 at 06:16
  • 1
    Point taken :), I truly didn't know, Reported :) nice reporting interface too – Shereef Marzouk May 12 '14 at 06:21
0

Please refer to the documentation.

You can register a ACTION_BATTERY_CHANGED receiver, and read out the extras such as:

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale;

From there, you can then act on batteryPct, whether it's 10, 20, 30 percent.

if( batteryPct <= 10 ) {
    // do 10% actions
} else if( batteryPct <= 20 ) {
    // do 20% actions
} else if( batteryPct <= 30 ) {
    // do 30% actions
} else {
    // do nothing
}

Be sure to keep track of what you've done, so you don't keep doing these things at 10%, 9%, 8%, etc.

323go
  • 14,143
  • 6
  • 33
  • 41
  • Thanks for your reply. but when will the ACTION_BATTERY_CHANGED receiver get called? I meant for every time the battery level changed? Like from level 60 to 59 will it call the receiver? – MCA Shah May 12 '14 at 06:41
  • 1
    That is correct. It'll be invoked for any change in Battery status. If you refer to the documentation, there's also an explanation how to just get the current level without registering a receiver. – 323go May 12 '14 at 12:40
  • Thanks for your reply, but my broadcast receiver is not getting called in battery level changed. In one post someone wrote that the receiver will not get called for each and every battery level. It differs from mobile to mobile. In some companies it get called every 10% down or up. Don't know but mine is not getting called anyway! – MCA Shah May 13 '14 at 03:13