作者:爱是一道菜lzb | 来源:互联网 | 2023-09-23 13:49
这里介绍电池信息的取得.android.content.BroadcastReceiver类android.os.BatteryManager类●电池信息的取得,调用register
这里介绍电池信息的取得.
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 sourceprint?
001 |
package com.adakoda.batterytest; |
003 |
import android.app.Activity; |
004 |
import android.content.BroadcastReceiver; |
005 |
import android.content.Context; |
006 |
import android.content.Intent; |
007 |
import android.content.IntentFilter; |
008 |
import android.os.BatteryManager; |
009 |
import android.os.Bundle; |
010 |
import android.util.Log; |
012 |
public class BatteryTestActivity extends Activity { |
013 |
/** Called when the activity is first created. */ |
015 |
public void onCreate(Bundle savedInstanceState) { |
016 |
super .onCreate(savedInstanceState); |
017 |
setContentView(R.layout.main); |
021 |
protected void onResume() { |
024 |
IntentFilter filter = new IntentFilter(); |
026 |
filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
027 |
registerReceiver(mBroadcastReceiver, filter); |
031 |
protected void onPause() { |
034 |
unregisterReceiver(mBroadcastReceiver); |
037 |
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
039 |
public void onReceive(Context context, Intent intent) { |
040 |
String action = intent.getAction(); |
041 |
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
042 |
int status = intent.getIntExtra( "status" , 0 ); |
043 |
int health = intent.getIntExtra( "health" , 0 ); |
044 |
boolean present = intent.getBooleanExtra( "present" , false ); |
045 |
int level = intent.getIntExtra( "level" , 0 ); |
046 |
int scale = intent.getIntExtra( "scale" , 0 ); |
047 |
int icon_small = intent.getIntExtra( "icon-small" , 0 ); |
048 |
int plugged = intent.getIntExtra( "plugged" , 0 ); |
049 |
int voltage = intent.getIntExtra( "voltage" , 0 ); |
050 |
int temperature = intent.getIntExtra( "temperature" , 0 ); |
051 |
String technology = intent.getStringExtra( "technology" ); |
053 |
String statusString = "" ; |
056 |
case BatteryManager.BATTERY_STATUS_UNKNOWN: |
057 |
statusString = "unknown" ; |
059 |
case BatteryManager.BATTERY_STATUS_CHARGING: |
060 |
statusString = "charging" ; |
062 |
case BatteryManager.BATTERY_STATUS_DISCHARGING: |
063 |
statusString = "discharging" ; |
065 |
case BatteryManager.BATTERY_STATUS_NOT_CHARGING: |
066 |
statusString = "not charging" ; |
068 |
case BatteryManager.BATTERY_STATUS_FULL: |
069 |
statusString = "full" ; |
073 |
String healthString = "" ; |
076 |
case BatteryManager.BATTERY_HEALTH_UNKNOWN: |
077 |
healthString = "unknown" ; |
079 |
case BatteryManager.BATTERY_HEALTH_GOOD: |
080 |
healthString = "good" ; |
082 |
case BatteryManager.BATTERY_HEALTH_OVERHEAT: |
083 |
healthString = "overheat" ; |
085 |
case BatteryManager.BATTERY_HEALTH_DEAD: |
086 |
healthString = "dead" ; |
088 |
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: |
089 |
healthString = "voltage" ; |
091 |
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: |
092 |
healthString = "unspecified failure" ; |
096 |
String acString = "" ; |
099 |
case BatteryManager.BATTERY_PLUGGED_AC: |
100 |
acString = "plugged ac" ; |
102 |
case BatteryManager.BATTERY_PLUGGED_USB: |
103 |
acString = "plugged usb" ; |
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); |
参考资源
● BroadcastReceiver | Android Developers
● BatteryManager | Android Developers