Android电池信息
Android中电池信息(Battery information)的取得
这里介绍电池信息的取得.
Android.content.BroadcastReceiver类
Android.os.BatteryManager类
● 电池信息的取得,调用registerReceiver()方法。
第1个参数,设置BroadcastReceiver实例
第2个参数,设置追加了Intent.ACTION_BATTERY_CHANGED处理的IntentFilter实例。
● 在BroadcastReceiver的onReceive()事件,接收到的Intent.ACTION_BATTERY_CHANGED,包括下面的信息。
“status”(int类型)…状态,定义值是BatteryManager.BATTERY_STATUS_XXX。
“health”(int类型)…健康,定义值是BatteryManager.BATTERY_HEALTH_XXX。
“present”(boolean类型)
“level”(int类型)…电池剩余容量
“scale”(int类型)…电池最大值。通常为100。
“icon-small”(int类型)…图标ID。
“plugged”(int类型)…连接的电源插座,定义值是BatteryManager.BATTERY_PLUGGED_XXX。
“voltage”(int类型)…mV。
“temperature”(int类型)…温度,0.1度单位。例如表示197的时候,意思为19.7度。
“technology”(String类型)…电池类型,例如,Li-ion等等。
[java]view plaincopyprint?
1. package dev.demo.batterytest;
2.
3. import Android.app.Activity;
4. import Android.content.BroadcastReceiver;
5. import Android.content.Context;
6. import Android.content.Intent;
7. import Android.content.IntentFilter;
8. import Android.os.BatteryManager;
9. import Android.os.Bundle;
10. import Android.util.Log;
11.
12. public class BatteryTestActivity extends Activity {
13. /** Called when the activity is first created. */
14. @Override
15. public void onCreate(Bundle savedInstanceState) {
16. super.onCreate(savedInstanceState);
17. setContentView(R.layout.main);
18. }
19.
20. @Override
21. protected void onResume() {
22. super.onResume();
23.
24. IntentFilter filter = new IntentFilter();
25.
26. filter.addAction(Intent.ACTION_BATTERY_CHANGED);
27. registerReceiver(mBroadcastReceiver, filter);
28. }
29.
30. @Override
31. protected void onPause() {
32. super.onPause();
33.
34. unregisterReceiver(mBroadcastReceiver);
35. }
36.
37. private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
38. @Override
39. public void onReceive(Context context, Intent intent) {
40. String action = intent.getAction();
41. if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
42. int status = intent.getIntExtra("status", 0);
43. int health = intent.getIntExtra("health", 0);
44. boolean present = intent.getBooleanExtra("present", false);
45. int level = intent.getIntExtra("level", 0);
46. int scale = intent.getIntExtra("scale", 0);
47. int icon_small = intent.getIntExtra("icon-small", 0);
48. int plugged = intent.getIntExtra("plugged", 0);
49. int voltage = intent.getIntExtra("voltage", 0);
50. int temperature = intent.getIntExtra("temperature", 0);
51. String technology = intent.getStringExtra("technology");
52.
53. String statusString = "";
54.
55. switch (status) {
56. case BatteryManager.BATTERY_STATUS_UNKNOWN:
57. statusString = "unknown";
58. break;
59. case BatteryManager.BATTERY_STATUS_CHARGING:
60. statusString = "charging";
61. break;
62. case BatteryManager.BATTERY_STATUS_DISCHARGING:
63. statusString = "discharging";
64. break;
65. case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
66. statusString = "not charging";
67. break;
68. case BatteryManager.BATTERY_STATUS_FULL:
69. statusString = "full";
70. break;
71. }
72.
73. String healthString = "";
74.
75. switch (health) {
76. case BatteryManager.BATTERY_HEALTH_UNKNOWN:
77. healthString = "unknown";
78. break;
79. case BatteryManager.BATTERY_HEALTH_GOOD:
80. healthString = "good";
81. break;
82. case BatteryManager.BATTERY_HEALTH_OVERHEAT:
83. healthString = "overheat";
84. break;
85. case BatteryManager.BATTERY_HEALTH_DEAD:
86. healthString = "dead";
87. break;
88. case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
89. healthString = "voltage";
90. break;
91. case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
92. healthString = "unspecified failure";
93. break;
94. }
95.
96. String acString = "";
97.
98. switch (plugged) {
99. case BatteryManager.BATTERY_PLUGGED_AC:
100.acString = "plugged ac";
101.break;
102.case BatteryManager.BATTERY_PLUGGED_USB:
103.acString = "plugged usb";
104.break;
105.}
106.
107.Log.v("status", statusString);
108.Log.v("health", healthString);
109.Log.v("present", String.valueOf(present));
110.Log.v("level", String.valueOf(level));
111.Log.v("scale", String.valueOf(scale));
112.Log.v("icon_small", String.valueOf(icon_small));
113.Log.v("plugged", acString);
114.Log.v("voltage", String.valueOf(voltage));
115.Log.v("temperature", String.valueOf(temperature));
116.Log.v("technology", technology);
117.}
118.}
119.};
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。