7

Is it possible to get the remaining battery time available from an Android phone?

Thanks.

jimmym715
  • 1,512
  • 1
  • 16
  • 25
Sodino
  • 587
  • 6
  • 16
  • what do you mean by "_remaing time_"? – Vladimir Jan 19 '12 at 09:14
  • Get the remaing time avaiable from a Android Phone's battery – Sodino Jan 19 '12 at 09:17
  • **Did you mean battery charge remaining time ?** please make question clear, don't post any questions to receive this type of comments, if you do so, you will get down votes and finally you may lose chance to ask further questions. – Yugandhar Babu Jan 19 '12 at 09:18
  • You can get current battery level from http://developer.android.com/reference/android/os/BatteryManager.html#EXTRA_LEVEL , but you need to find a way to estimate "remaining time" based on some criteria – Vladimir Jan 19 '12 at 09:20
  • @Sodino how u resolved ur issue ? – Erum Jan 05 '15 at 09:58

3 Answers3

5

There are a few apps (battery widgets and the like) that estimate time remaining before the battery runs out. I'd guess they take current battery level and try to work out current drain based on processor usage, backlight level etc. In my experience they aren't too accurate but in principle if you get the algorithm right it should be possible.

Jaloopa
  • 722
  • 1
  • 6
  • 21
4

You can get battery life with help of broadcast receiver by registering a receiver for action Intent.ACTION_BATTERY_CHANGED. My answer is key only, get information from Android Developers website.

By using the below statement in onReceive() method of BroadcastReceiver with above Intent action, you will get battery level currently available(e.g., 50%, 60%, etc.). But you can't estimate the time remaining, because some apps may consume more power. So i think battery level to time remaining conversion won't give correct result.

battery_level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

I hope it may help you.

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
0

Got it! You can just catch information from dumpsys but it requires ROOT permission. I Can execute command & get output with this:

fun execRoot(command: String): Pair<String, Boolean> {
    return try {
        val process = Runtime.getRuntime().exec(
            arrayOf(
                "su",
                "-c",
                "cd / && $command"
            )
        )
        val reader = BufferedReader(InputStreamReader(process.inputStream))
        var line: String?
        var final = ""
        while (reader.readLine().also { line = it } != null) {
            final = "$final$line\n"
        }
        return Pair(final, true)
    } catch (e: IOException) {
        Pair("", false)
    }
}

With this function I can catch output from dumpsys. You can get info about time remaining with this line:

var line = execRoot("dumpsys batterystats | grep -E \"Battery time remaining\"").first

It will return Battery time remaining: 1h 55m 41s 682ms, if have 0 percents - Battery time remaining: 0ms, if phone is charging it will return nothing

Renattele Renattele
  • 1,626
  • 2
  • 15
  • 32