服务器之家

服务器之家 > 正文

Android监听电池状态实例代码

时间:2021-06-22 14:11     来源/作者:Android开发网

如果要监听电池的状态改变,需要动态注册:android.intent.action.battery_changed,收到action后可以根据对应的key获取你需要的信息,更详细信息可以参考以下例子中的batterychangedreceiver类

具体代码如下所示:

?
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
package com.example.charginganimation;
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.view.view;
public class mainactivity extends activity {
private customcliploading ccl;
private batterychangedreceiver receiver = new batterychangedreceiver();
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
registerreceiver(receiver, getfilter());
}
private intentfilter getfilter() {
intentfilter filter = new intentfilter();
filter.addaction(intent.action_battery_changed);
filter.addaction(intent.action_battery_low);
filter.addaction(intent.action_battery_okay);
return filter;
}
@override
protected void ondestroy() {
// todo auto-generated method stub
unregisterreceiver(receiver);
super.ondestroy();
}
class batterychangedreceiver extends broadcastreceiver {
@override
public void onreceive(context context, intent intent) {
// todo auto-generated method stub
final string action = intent.getaction();
if (action.equalsignorecase(intent.action_battery_changed)) {
system.out
.println("batterychangedreceiver battery_changed_action---");
// 当前电池的电压
int voltage = intent.getintextra(batterymanager.extra_voltage,
-1);
// 电池的健康状态
int health = intent
.getintextra(batterymanager.extra_health, -1);
switch (health) {
case batterymanager.battery_health_cold:
system.out.println("battery_health_cold");
break;
case batterymanager.battery_health_dead:
system.out.println("battery_health_dead ");
break;
case batterymanager.battery_health_good:
system.out.println("battery_health_good");
break;
case batterymanager.battery_health_overheat:
system.out.println("battery_health_overheat");
break;
case batterymanager.battery_health_over_voltage:
system.out.println("battery_health_cold");
break;
case batterymanager.battery_health_unknown:
system.out.println("battery_health_unknown");
break;
case batterymanager.battery_health_unspecified_failure:
system.out.println("battery_health_unspecified_failure");
break;
default:
break;
}
// 电池当前的电量, 它介于0和 extra_scale之间
int level = intent.getintextra(batterymanager.extra_level, -1);
// 电池电量的最大值
int scale = intent.getintextra(batterymanager.extra_scale, -1);
// 当前手机使用的是哪里的电源
int pluged = intent.getintextra(batterymanager.extra_plugged,
-1);
switch (pluged) {
case batterymanager.battery_plugged_ac:
// 电源是ac charger.[应该是指充电器]
system.out.println("battery_plugged_ac");
break;
case batterymanager.battery_plugged_usb:
// 电源是usb port
system.out.println("battery_plugged_usb ");
break;
default:
break;
}
int status = intent
.getintextra(batterymanager.extra_status, -1);
switch (status) {
case batterymanager.battery_status_charging:
// 正在充电
system.out.println("battery_status_charging ");
break;
case batterymanager.battery_status_discharging:
system.out.println("battery_status_discharging ");
break;
case batterymanager.battery_status_full:
// 充满
system.out.println("battery_status_full ");
break;
case batterymanager.battery_status_not_charging:
// 没有充电
system.out.println("battery_status_not_charging ");
break;
case batterymanager.battery_status_unknown:
// 未知状态
system.out.println("battery_status_unknown ");
break;
default:
break;
}
// 电池使用的技术。比如,对于锂电池是li-ion
string technology = intent
.getstringextra(batterymanager.extra_technology);
// 当前电池的温度
int temperature = intent.getintextra(
batterymanager.extra_temperature, -1);
system.out.println("voltage = " + voltage + " technology = "
+ technology + " temperature = " + temperature
+ " level = " + level + " scale = " + scale);
} else if (action.equalsignorecase(intent.action_battery_low)) {
// 表示当前电池电量低
system.out
.println("batterychangedreceiver action_battery_low---");
} else if (action.equalsignorecase(intent.action_battery_okay)) {
// 表示当前电池已经从电量低恢复为正常
system.out
.println("batterychangedreceiver action_battery_okay---");
}
}
}
}

获取apk和版本信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private int getvercode(context context) {
int vercode = -1;
try {
vercode = context.getpackagemanager().getpackageinfo(
"com.example.charginganimation", 0).versioncode;
} catch (namenotfoundexception e) {
e.printstacktrace();
}
return vercode;
}
private string getvername(context context) {
string vername = "";
try {
vername = context.getpackagemanager().getpackageinfo(
"com.example.charginganimation", 0).versionname;
} catch (namenotfoundexception e) {
e.printstacktrace();
}
return vername;
}

以上代码内容是小编给大家介绍的android监听电池状态的实例代码,本文写的不好还请见谅,有好的意见欢迎给小编留言,我会及时给大家沟通的,谢谢大家一直以来对服务器之家网站的支持!

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部