How to calculate remaining time of android phone b

2019-02-23 14:59发布

问题:

This question already has an answer here:

  • Calculating remaining time of battery live 3 answers

I am developing battery health app on Android Studio. I want to know how to calculate remaining time by playing music or video, browsing website, or standby. I have seen many android apps like this but can not see the source code. I want to see the sample code for calculating remaining time for playing music or video and so on. If you have developed android battery healthy app before, please share your knowledge.

回答1:

-- Simplest and most easiest way to calculate remaining battery life :

MainActivity.java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private TextView batteryPercent;
    private void getBatteryPercentage() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (currentLevel >= 0 && scale > 0) {
                    level = (currentLevel * 100) / scale;
                }
                batteryPercent.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        batteryPercent = (TextView) this.findViewById(R.id.batteryLevel);
        getBatteryPercentage();
    }
}

xml file :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/batteryLevel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:textSize="20dip">
    </TextView>

</RelativeLayout>


回答2:

You can not accurately guess that how much time is remaining for battery discharge, because there might be different applications or service consuming battery.

However you can get battery life with help of broadcast receiver by registering a receiver for action Intent.ACTION_BATTERY_CHANGED.

By using the below statement in onReceive() method of BroadcastReceiver with above Intent action, you will get battery level currently available. But you can't estimate the time remaining, because some apps may consume more power.

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


回答3:

The following code will help you to calculate battery level

public int getBatteryLevel(Context context) {
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    return level;
}