具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<?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 获取电池的信息实例代码,希望对大家有所帮助!