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

Andriod 获取电池的信息实例代码

程序员文章站 2024-02-26 18:37:10
具体代码如下所示:

具体代码如下所示:

<?xml version="1.0"?>
<linearlayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="获取电池的信息" android:id="@+id/btn_battery"/>
<textview android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tv_battery"/>
</linearlayout> 
package com.example.yanlei.wifi;
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.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.textview;
public class mainactivity extends appcompatactivity {
// 定义电池信息的按钮
private button btnbattery;
// 定义显示电池信息的textview
private textview tvbattery;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
// 得到布局中的所有对象
findview();
// 设置对象的监听器
setlistener();
}
private void findview() {
// 得到布局中的所有对象
btnbattery = (button) findviewbyid(r.id.btn_battery);
tvbattery = (textview) findviewbyid(r.id.tv_battery);
}
// 设置对象的监听器
private void setlistener() {
btnbattery.setonclicklistener(listener);
}
onclicklistener listener = new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
switch (v.getid()) {
// 当前的音量
case r.id.btn_battery:
intentfilter filter = new intentfilter();
filter.addaction(intent.action_battery_changed);
registerreceiver(mbroadcastreceiver, filter);
break;
}
}
};
// 声明广播接受者对象
private broadcastreceiver mbroadcastreceiver = new broadcastreceiver() {
@override
public void onreceive(context context, intent intent) {
// todo auto-generated method stub
string action = intent.getaction();
if (action.equals(intent.action_battery_changed)) {
// 得到电池状态:
// batterymanager.battery_status_charging:充电状态。
// batterymanager.battery_status_discharging:放电状态。
// batterymanager.battery_status_not_charging:未充满。
// batterymanager.battery_status_full:充满电。
// batterymanager.battery_status_unknown:未知状态。
int status = intent.getintextra("status", 0);
// 得到健康状态:
// batterymanager.battery_health_good:状态良好。
// batterymanager.battery_health_dead:电池没有电。
// batterymanager.battery_health_over_voltage:电池电压过高。
// batterymanager.battery_health_overheat:电池过热。
// batterymanager.battery_health_unknown:未知状态。
int health = intent.getintextra("health", 0);
// boolean类型
boolean present = intent.getbooleanextra("present", false);
// 得到电池剩余容量
int level = intent.getintextra("level", 0);
// 得到电池最大值。通常为100。
int scale = intent.getintextra("scale", 0);
// 得到图标id
int icon_small = intent.getintextra("icon-small", 0);
// 充电方式: batterymanager.battery_plugged_ac:ac充电。 batterymanager.battery_plugged_usb:usb充电。
int plugged = intent.getintextra("plugged", 0);
// 得到电池的电压
int voltage = intent.getintextra("voltage", 0);
// 得到电池的温度,0.1度单位。例如 表示197的时候,意思为19.7度
int temperature = intent.getintextra("temperature", 0);
// 得到电池的类型
string technology = intent.getstringextra("technology");
// 得到电池状态
string statusstring = "";
// 根据状态id,得到状态字符串
switch (status) {
case batterymanager.battery_status_unknown:
statusstring = "unknown";
break;
case batterymanager.battery_status_charging:
statusstring = "charging";
break;
case batterymanager.battery_status_discharging:
statusstring = "discharging";
break;
case batterymanager.battery_status_not_charging:
statusstring = "not charging";
break;
case batterymanager.battery_status_full:
statusstring = "full";
break;
}
//得到电池的寿命状态
string healthstring = "";
//根据状态id,得到电池寿命
switch (health) {
case batterymanager.battery_health_unknown:
healthstring = "unknown";
break;
case batterymanager.battery_health_good:
healthstring = "good";
break;
case batterymanager.battery_health_overheat:
healthstring = "overheat";
break;
case batterymanager.battery_health_dead:
healthstring = "dead";
break;
case batterymanager.battery_health_over_voltage:
healthstring = "voltage";
break;
case batterymanager.battery_health_unspecified_failure:
healthstring = "unspecified failure";
break;
}
//得到充电模式
string acstring = "";
//根据充电状态id,得到充电模式
switch (plugged) {
case batterymanager.battery_plugged_ac:
acstring = "plugged ac";
break;
case batterymanager.battery_plugged_usb:
acstring = "plugged usb";
break;
}
//显示电池信息
tvbattery.settext("电池的状态:" + statusstring
+ "\n健康值: "+ healthstring
+ "\n电池剩余容量: " + level
+ "\n电池的最大值:" + scale
+ "\n小图标:" + icon_small
+ "\n充电方式:" + plugged
+ "\n充电方式: " + acstring
+ "\n电池的电压:" + voltage
+ "\n电池的温度:" + (float) temperature * 0.1
+ "\n电池的类型:" + technology);
}
}
};
@override
protected void onpause() {
super.onpause();
// 解除注册监听
unregisterreceiver(mbroadcastreceiver);
}
}

以上所述是小编给大家介绍的andriod 获取电池的信息实例代码,希望对大家有所帮助!