來源: http://www.5look.net/blog/archives/156
Ref. :
● BroadcastReceiver | Android Developers
● BatteryManager | Android Developers
這裡介紹電池信息的取得.
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等等。
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); |
留言列表