安卓系统通过广播来接受电池状态,下面是我写的一个简单电池管理项目。
广播接受类BatteryReceiver如下:
package com.lml.batteryshow;
import java.util.HashMap;
import java.util.Map;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Message;
public class BatteryReceiver extends BroadcastReceiver {
Message message;
Map map = null;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
map = new HashMap();
/** 电池剩余电量 */
map.put("level", intent.getIntExtra("level", 0)+"");
/** 获取电池满电量数值 */
map.put("scale", intent.getIntExtra("scale", 0)+"");
/** 获取电池技术支持 */
map.put("technology", intent.getStringExtra("technology"));
/** 获取电池状态 */
map.put("status", intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)+"");
/** 获取电源信息 */
map.put("plugType", intent.getIntExtra("plugged", 0)+"");
/** 获取电池健康度 */
map.put("health", intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)+"");
/** 获取电池电压 */
map.put("voltage", intent.getIntExtra("voltage", 0) + "");
/** 获取电池温度 */
map.put("temperature", intent.getIntExtra("temperature", 0) + "");
}
if (map != null) {
message = new Message();
message.obj = map;
MainActivity.handler1.dispatchMessage(message);
}
}
}
主类MainActivity代码实现如下:
package com.lml.batteryshow;
import java.text.DecimalFormat;
import java.util.Map;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;
public class MainActivity extends Activity {
/** 电池状态 */
private TextView battery_Status;
/** 电池允许状态 */
private TextView battery_Run_Status;
/** 电池电压 */
private TextView battery_Voltage;
/** 电池温度 */
private TextView battery_Temperature;
/** 电池技术 */
private TextView battery_Technology;
/** 电池运行时间 */
private TextView battery_Time;
/** 电池电量 */
private TextView battery_Level;
Thread thread;
public static Handler handler1;
public Handler handler2;
BatteryReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
thread= new Thread(runnable);
thread.start();
regReceiver();
handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
MainActivity.this.handleMessage(msg);
}
};
/** 设置启动时间 */
handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
battery_Time.setText("启动后时间:"+msg.obj.toString());
}
};
}
private void regReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
receiver=new BatteryReceiver();
registerReceiver(receiver, filter);
}
private void handleMessage(Message msg){
@SuppressWarnings("unchecked")
Map map = (Map) msg.obj;
String plugType=map.get("plugType");
/** 设置电池状态 为USB充电 */
if (plugType.equals("2")) {
battery_Status.setText("电池状态:正在充电(USB)");
}
/** 设置电池状态 为交流电充电 */
else if (plugType.equals("1")) {
battery_Status.setText("电池状态:正在充电(交流电)");
}
/** 设置电池状态 */
else if (plugType.equals("0")) {
battery_Status.setText("电池状态:正在耗电");
}
/** 获取电池健康度 */
String health = map.get("health");
if (health.equals(2)) {
battery_Run_Status.setText("电池健康度;正常");
} else {
battery_Run_Status.setText("电池健康度;一般");
}
/** 设置电池电压 */
battery_Voltage.setText( "电池电压:"+map.get("voltage") + "mV");
/** 设置电池温度 */
double temp = Double.parseDouble(map.get("temperature") );
battery_Temperature.setText("电池温度:"+ (temp / 10) + "°C");
/** 获取电池满电量数值 */
double scale = Double.parseDouble(map.get("scale") );
/** 获取电池剩余电量 */
double level = Double.parseDouble(map.get("level") );
double per =level/scale;
String strPer=optionNum(per);
/** 设置电池电量 */
battery_Level.setText("电池电量:"+Double.parseDouble(strPer) * 100 + "%");
/** 设置电池技术 */
battery_Technology.setText( "电池技术:"+map.get("technology"));
}
@Override
protected void onDestroy() {
super.onDestroy();
thread.interrupt();
unregisterReceiver(receiver);
}
public String optionNum(double f) {
DecimalFormat df = new DecimalFormat("#.00");
return df.format(f);
}
private void init() {
/** 获取电池状态对象 */
battery_Status = (TextView) findViewById(R.id.battery_status);
/** 获取电池运行状态 */
battery_Run_Status = (TextView) findViewById(R.id.battery_health);
/** 获取电池电压 */
battery_Voltage = (TextView) findViewById(R.id.battery_voltage);
/** 获取电池温度 */
battery_Temperature = (TextView) findViewById(R.id.battery_temperature);
/** 获取电池技术 */
battery_Technology = (TextView) findViewById(R.id.battery_technology);
/** 开机电池运行时间 */
battery_Time = (TextView) findViewById(R.id.boot_time);
/** 获取电池电量对象 */
battery_Level = (TextView) findViewById(R.id.battery);
}
Runnable runnable=new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
long temp = SystemClock.elapsedRealtime();
Message message = new Message();
message.obj = Util.transitionTime(temp);
handler2.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
帮助类util如下:
package com.lml.batteryshow;
public class Util {
public static String transitionTime(long time) {
long temp1 = time / 1000;
long temp2 = temp1 / 60;
long temp3 = temp2 / 60;
String h = temp3 % 60 + "";
String m = temp2 % 60 + "";
String s = temp1 % 60 + "";
if (m.length() < 2) {
m = "0" + m;
}
if (s.length() < 2) {
s = "0" + s;
}
String time1 = h + ":" + m + ":" + s;
System.out.println(time1);
return time1;
}
}
布局文件activity_main.xml如下:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="50dp"
android:gravity="center_vertical">
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电池状态"
android:textSize="20sp"
android:layout_marginBottom="40dp"/>
android:id="@+id/battery_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/battery_health"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/battery_voltage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/battery_temperature"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:id="@+id/battery_technology"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:id="@+id/battery"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:id="@+id/boot_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这是简单的实现的电源管理。