欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android电池充电显示数据

程序员文章站 2024-03-22 18:25:40
...

今天给自己的手机充电,老是判断是交流电充电还是USB充电,于是想做个APP显示充电时的数据,不说了,直接上代码。

package com.example.powerbattery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView t1;
    String plug;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t1 = (TextView)findViewById(R.id.tx1);

        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = registerReceiver(null, ifilter);

        // 是否在充电
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING||
                status == BatteryManager.BATTERY_STATUS_FULL;

        // 怎么充
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);

        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCarge =chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
        if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB){
            plug = "USB";
        }else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC){
            plug = "交流电";
        }

        //当前剩余电量
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        //电量最大值
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    //    Double i = batteryStatus.getDoubleExtra(BatteryManager.EXTRA_SCALE, -1);
        //电量百分比
        float batteryPct = level / (float)scale;

        int temperature= batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
        // integer containing the current battery voltage level.
        int voltage = batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);

        int health = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
        String healthStr = "";
        switch (health){
            case BatteryManager.BATTERY_HEALTH_COLD:
                healthStr="cold";
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD:
                healthStr="dead";
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                healthStr="good";
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                healthStr="voer voltage";
                break;
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                healthStr="unknown";
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                healthStr="unspecified failure";
                break;
        }

        t1.setText("是否在充电:" + isCharging);
        t1.append("\n是否USB充电:" + usbCharge);
        t1.append("\n是否交流电充电:" + acCarge);
        t1.append("\n充电方式:" + plug);
        t1.append("\n电量百分比:" + (batteryPct * 100) + "%");
        t1.append("\n电池状态:"+healthStr);
        t1.append("\n电池温度:"+temperature);
        t1.append("\n电池电压:"+voltage);

    }

}

结果图:
Android电池充电显示数据