BatteryReceiver.java 广播机制
View Code
package com.example.batterydemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.BatteryManager; import android.util.Log; public class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); // 要看看是不是我们要处理的消息 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { // 电池电量,数字 Log.i("Battery", "★电池电量★:" + intent.getIntExtra("level", 0)); // 电池最大容量 Log.i("Battery", "★电池最大容量★:" + intent.getIntExtra("scale", 0)); // 电池伏数,电池的电压 Log.i("Battery", "★电池伏数★:" + intent.getIntExtra("voltage", 0)); // 电池温度(单位:摄氏度) Log.i("Battery", "★电池温度★:" + intent.getIntExtra("temperature", 0)); // 取得电池类型 Log.i("Battery", "★电池类型★:" + intent.getStringExtra("technology")); // 取得电池对应的图标ID Log.i("Battery", "★取得电池对应的图标ID★:" + intent.getIntExtra("icon-small", 0)); /** 电池状态,返回是一个数字 */ // BatteryManager.BATTERY_STATUS_CHARGING 表示是充电状态 // BatteryManager.BATTERY_STATUS_NOT_CHARGING 表示电池不充电状态 // BatteryManager.BATTERY_STATUS_DISCHARGING 放电中 // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充电 // BatteryManager.BATTERY_STATUS_FULL 电池满 Log.i("Battery","★电池状态★:" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)); Log.i("Battery", "充电状态:" + BatteryManager.BATTERY_STATUS_CHARGING); Log.i("Battery", "电池不充电状态:" + BatteryManager.BATTERY_STATUS_NOT_CHARGING); Log.i("Battery", "放电中:" + BatteryManager.BATTERY_STATUS_DISCHARGING); Log.i("Battery", "未充电:" + BatteryManager.BATTERY_STATUS_NOT_CHARGING); Log.i("Battery", "电池满:" + BatteryManager.BATTERY_STATUS_FULL); /** 充电类型 */ Log.i("Battery", "★充电类型★ :" + intent.getIntExtra("plugged", 0)); Log.i("Battery", "交流电电源:" + BatteryManager.BATTERY_PLUGGED_AC); Log.i("Battery", "USB电源:" + BatteryManager.BATTERY_PLUGGED_USB); /** 电池健康情况,返回也是一个数字 */ // BatteryManager.BATTERY_HEALTH_GOOD 电池健康 // BatteryManager.BATTERY_HEALTH_OVERHEAT 电池过热 // BatteryManager.BATTERY_HEALTH_DEAD 电池损毁 // BatteryManager.BATTERY_HEALTH_UNKNOWN 未知状态 // BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 电池电压过大 // BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未明示故障 Log.i("Battery", "★电池健康情况★:" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)); Log.i("Battery", "电池健康:" + BatteryManager.BATTERY_HEALTH_GOOD); Log.i("Battery", "电池过热:" + BatteryManager.BATTERY_HEALTH_OVERHEAT); Log.i("Battery", "电池损毁:" + BatteryManager.BATTERY_HEALTH_DEAD); Log.i("Battery", "未知状态:" + BatteryManager.BATTERY_HEALTH_UNKNOWN); Log.i("Battery", "电池电压过大:" + BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE); Log.i("Battery", "未明示故障:" + BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE); } } }
BatteryActivity.java Activity页面
View Code
package com.example.batterydemo; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; public class BatteryActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.main); BatteryReceiver breceiver = new BatteryReceiver(); IntentFilter intentFinter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); BatteryActivity.this.registerReceiver(breceiver, intentFinter); } }
AndroidManifest.xml 配置文件
View Code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.batterydemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".BatteryActivity" android:label="@string/app_name" android:theme="@style/FullscreenTheme" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BatteryReceiver" /> </application> </manifest>