如何计算android手机电池的剩余时间? [英] How to calculate remaining time of android phone battery?

查看:197
本文介绍了如何计算android手机电池的剩余时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android Studio上开发电池健康应用程序.我想知道如何通过播放音乐或视频,浏览网站或待机来计算剩余时间.我已经看过很多这样的android应用,但是看不到源代码.我想查看用于计算播放音乐或视频等剩余时间的示例代码.如果您以前开发过android电池健康应用,请分享您的知识.

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.

推荐答案

-计算剩余电池寿命的最简单,最简单的方法:

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

MainActivity.java:

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文件:

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

这篇关于如何计算android手机电池的剩余时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆