我们在开发某些安卓应用时需要获取电池的一些状态信息如:电量、电压、温度等,虽然在安卓操作系统中已有查看电池信息的功能,但需要手动打开查看,无法在我们想要的界面上显示,本文所要讲的就是如何让电池信息动态显示到我们所开发应用的界面中。
首先,我们需要确定要获取哪些电池信息,这里主要获取五项信息:电池电量、电池电压、电池温度、电池状态、电池使用情况。
其次,我们利用广播接收机制来实时获取电池信息:registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)),利用TextView来显示这些信息。
最后,创建广播接收器,里面定义相关的信息值及它们的显示条件,根据合适的条件来打印出这些信息。
下面给出相关代码:
BatteryTestDemoActivity.java
主类
<span style="font-size:14px;">package com.cfzz.bi;
import android.app.Activity;
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.widget.TextView;
public class BatteryTestDemoActivity extends Activity
{
private int BatteryN; //目前电量
private int BatteryV; //电池电压
private double BatteryT; //电池温度
private String BatteryStatus; //电池状态
private String BatteryTemp; //电池使用情况
public TextView TV;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 注册一个系统 BroadcastReceiver,作为访问电池信息之用,这个不能直接在AndroidManifest.xml中注册
registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
TV = (TextView)findViewById(R.id.TV);
}
/* 创建广播接收器 */
public BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
//如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()
if (Intent.ACTION_BATTERY_CHANGED.equals(action))
{
BatteryN = intent.getIntExtra("level", 0); //目前电量(0~100)
BatteryV = intent.getIntExtra("voltage", 0); //电池电压(mv)
BatteryT = intent.getIntExtra("temperature", 0); //电池温度(数值)
double T = BatteryT/10.0; //电池摄氏温度,默认获取的非摄氏温度值,需做一下运算转换
switch (intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN))
{
case BatteryManager.BATTERY_STATUS_CHARGING:
BatteryStatus = "充电状态";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
BatteryStatus = "放电状态";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
BatteryStatus = "未充电";
break;
case BatteryManager.BATTERY_STATUS_FULL:
BatteryStatus = "充满电";
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
BatteryStatus = "未知道状态";
break;
}
switch (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN))
{
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
BatteryTemp = "未知错误";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
BatteryTemp = "状态良好";
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
BatteryTemp = "电池没有电";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
BatteryTemp = "电池电压过高";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
BatteryTemp = "电池过热";
break;
}
TV.setText("Level: " + BatteryN + "%" +"\n" + "Voltage: " + BatteryV + "mV" + "\n" + "Temperature: " + T + "℃" + "\n" + "Status: " + BatteryTemp + "---" + BatteryStatus);
}
}
};
}</span><span style="font-size: 18px;">
</span>
main.xml
定义显示布局
<span style="font-size:14px;"><?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:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp"
android:textSize="22dp" />
</RelativeLayout></span>
AndroidManifest.xml
注意添加权限
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cfzz.bi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<span style="background-color: rgb(51, 204, 255);"><uses-permission android:name="android.permission.READ_FRAME_BUFFER"/></span>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name="org.achartengine.GraphicalActivity" />
<activity
android:name="com.cfzz.bi.BatteryTestDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest></span>
运行效果: