原文地址:http://blog.csdn.net/sharp2wing/archive/2010/08/20/5826493.aspx
这里介绍电池信息的取得.
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)
001 package com.adakoda.batterytest; 002 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; 011 012 public class BatteryTestActivity extends Activity { 013 /** Called when the activity is first created. */ 014 @Override 015 public void onCreate(Bundle savedInstanceState) { 016 super.onCreate(savedInstanceState); 017 setContentView(R.layout.main); 018 } 019 020 @Override 021 protected void onResume() { 022 super.onResume(); 023 024 IntentFilter filter = new IntentFilter(); 025 026 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 027 registerReceiver(mBroadcastReceiver, filter); 028 } 029 030 @Override 031 protected void onPause() { 032 super.onPause(); 033 034 unregisterReceiver(mBroadcastReceiver); 035 } 036 037 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 038 @Override 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"); 052 053 String statusString = ""; 054 055 switch (status) { 056 case BatteryManager.BATTERY_STATUS_UNKNOWN: 057 statusString = "unknown"; 058 break; 059 case BatteryManager.BATTERY_STATUS_CHARGING: 060 statusString = "charging"; 061 break; 062 case BatteryManager.BATTERY_STATUS_DISCHARGING: 063 statusString = "discharging"; 064 break; 065 case BatteryManager.BATTERY_STATUS_NOT_CHARGING: 066 statusString = "not charging"; 067 break; 068 case BatteryManager.BATTERY_STATUS_FULL: 069 statusString = "full"; 070 break; 071 } 072 073 String healthString = ""; 074 075 switch (health) { 076 case BatteryManager.BATTERY_HEALTH_UNKNOWN: 077 healthString = "unknown"; 078 break; 079 case BatteryManager.BATTERY_HEALTH_GOOD: 080 healthString = "good"; 081 break; 082 case BatteryManager.BATTERY_HEALTH_OVERHEAT: 083 healthString = "overheat"; 084 break; 085 case BatteryManager.BATTERY_HEALTH_DEAD: 086 healthString = "dead"; 087 break; 088 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: 089 healthString = "voltage"; 090 break; 091 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: 092 healthString = "unspecified failure"; 093 break; 094 } 095 096 String acString = ""; 097 098 switch (plugged) { 099 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 }; 120 }
参考资源
● BroadcastReceiver | Android Developers
● BatteryManager | Android Developers
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sharp2wing/archive/2010/08/20/5826493.aspx