How to calculate remaining time of android phone battery? [duplicate]

二次信任 提交于 2019-12-01 19:05:48

问题


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


来源:https://stackoverflow.com/questions/35916446/how-to-calculate-remaining-time-of-android-phone-battery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!