引言
初學Andorid,學習了包括Activity、Service、AppWidget、Broadcast、Intent、Notification、XML布局、XML配置等基礎知識之後,嘗試著做一個小玩意兒——電池監控,期間也是阻撓多多,現在基本完成了,所以把自己的經驗分享給大家,希望能通過我的文章讓還不會的朋友少走彎路,也算是這個小玩意兒的一點貢獻。
由於時間有限,bill假設讀者已經具備並能基本使用上述基礎知識,本文將只描述如何利用上述知識構建一個監控Android電池電量的小程序(若有相關知識不甚清楚,請查詢Android SDK自帶的document,官方文檔是我們學習Android的一大利器),若有任何疑問或建議,歡迎留言提出,bill會很高興和大家一起學習進步!
開發環境及Android平台版本Eclipse helios
Android SDK 1.6
Android SDK 1.6 及以上模擬器或者Android SDK 1.6及以上Android X86 虛擬機或真機
功能描述由於Android自帶的系統電池圖標並不能顯示當前電量,給不少用戶帶來了不便,所以本程序將對此進行改進,簡單實現對Android手機電量的實時監控,並在左上角狀態欄顯示實時電池電量的通知圖標,若正在充電,則顯示充電狀態。為方便起見,還為本程序制作了一個AppWidget,成為一個簡單的桌面小工具。
相關附件下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/1月/29日/Android電池電量監控初步/
整體思路概覽
①由於要顯示電池圖標,因此需要一組對應的小icon,程序算出當前電量之後,就在狀態欄上顯示對應的icon即可。本程序用到的icon已經提供在附件,下載後將裡面的icons直接復制到res\drawable-hdpi中即可。
②由於電池更新是一個長期存在的過程,因此需要將我們的程序注冊為系統服務,以便長期生存於Android系統中。
③服務不會自行啟動,因此我們需要為自己的服務創建一個Activity,在第一次啟動本程序的Activity時創建並啟動我們的監控服務,之後的事便交由服務處理,Activity暫時未有太大作用(之後還可以在Activity中顯示電池當前的各種基本屬性等等,本文不作介紹),當然,我們也可以不要Activity,直接將我們的服務設置為開機啟動即可,關於開機啟動的事項已在【Android開機啟動Activity或Service的方法】一文中詳細描述,恕不贅述。
④為了方便起見,應該制作本程序的桌面小工具,也就用到了AppWidget方面的知識,稍後一一道來。
分而治之
小麻雀的心髒——電池監控服務MonitorService我們將要看到的是這個小麻雀的心髒——電池監控服務,本服務自啟動至停止,始終通過接受來自系統的廣播ACTION_BATTERY_CHANGED(本廣播指示電量發生了改變)來判斷並顯示當前電量。
下面是本Service的完整代碼,我會在可能費解的地方批注,希望能幫助大家理解。
- package com.billhoo.study;
-
- import android.app.Notification; //通知,即顯示在屏幕左上角的小圖標
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.BatteryManager;
- import android.os.IBinder;
-
- public class MonitorService extends Service {
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
-
- @Override
- public void onStart(Intent intent, int startId) {
- // 定義電池電量更新廣播的過濾器,只接受帶有ACTION_BATTERRY_CHANGED事件的Intent
- IntentFilter batteryChangedReceiverFilter = new IntentFilter();
- batteryChangedReceiverFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
-
- // 向系統注冊batteryChangedReceiver接收器,本接收器的實現見代碼字段處
- registerReceiver(batteryChangedReceiver, batteryChangedReceiverFilter);
-
- // 實例化Notification通知的管理器,即字段notification manager
- notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
-
- // 由於初始化本服務時系統可能沒有發出ACTION_BATTERY_CHANGED廣播,那麼剛才注冊的那個接收器將不會在本服務啟動時被激活,這種情況下就無法顯示當前電量,因此在這裡添加一個匿名廣播接收器。
- new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- int level = intent.getIntExtra("level", 0); //電池電量等級
- int scale = intent.getIntExtra("scale", 100); //電池滿時百分比
- int status = intent.getIntExtra("status", 0); //電池狀態
-
- // 若正在充電
- if (status == BatteryManager.BATTERY_STATUS_CHARGING)
- notification = getNotification(getChargingIcon(level * 100 / scale),
- "電池監控", System.currentTimeMillis(), "電池電量", "正在充電");
- else
- notification = getNotification(getStateIcon(level * 100 / scale),
- "電池監控", System.currentTimeMillis(), "電池電量", "請及時充電");
-
- //向系統發送此通知,為方便起見,通知ID簡單設為0
- notifyManager.notify(0, notification);
- }
- };
- }
-
- @Override
- public void onDestroy() {
- //當本服務終止時,通過通知ID注銷左上角的通知
- notifyManager.cancel(0);
- }
-
- // --------------------------------------私有方法------------------------------
- // 獲得一個notification
- private Notification getNotification(int notificationIcon, String tickerText,
- long when, String contentTitle, String contentText) {
- // 定義該notification的擴展文本和intent
- Notification notification = new Notification(notificationIcon, tickerText,
- when);
- Context context = getApplicationContext();
- Intent notificationIntent = new Intent(context, BatteryInfo.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
- notificationIntent, 0);
- notification.setLatestEventInfo(context, contentTitle, contentText,
- contentIntent);
-
- // 設置本狀態出現在“正在進行”而非“通知”欄目,不允許用戶清除狀態圖標
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- return notification;
- }
-
- // 獲得對應的電池狀態圖標
- private int getStateIcon(int batteryHealth) {
- if (batteryHealth >= 0 || batteryHealth <= 100)
- return batteryStateIcons[batteryHealth]; //這裡是為了方便起見而硬編碼的資源數組,直接從R.java文件中提取的十六進制資源ID
- return R.drawable.stat_sys_battery_unknown;
- }
-
- // 獲得對應的充電狀態圖標
- private int getChargingIcon(int batteryHealth) {
- if (batteryHealth >= 0 && batteryHealth < 5)
- return R.drawable.stat_sys_battery_charge_anim0;
- if (batteryHealth >= 5 && batteryHealth < 10)
- return R.drawable.stat_sys_battery_charge_anim01;
- if (batteryHealth >= 10 && batteryHealth < 15)
- return R.drawable.stat_sys_battery_charge_anim02;
- if (batteryHealth >= 15 && batteryHealth < 20)
- return R.drawable.stat_sys_battery_charge_anim03;
- if (batteryHealth >= 20 && batteryHealth < 25)
- return R.drawable.stat_sys_battery_charge_anim04;
- if (batteryHealth >= 25 && batteryHealth < 30)
- return R.drawable.stat_sys_battery_charge_anim05;
- if (batteryHealth >= 30 && batteryHealth < 35)
- return R.drawable.stat_sys_battery_charge_anim06;
- if (batteryHealth >= 35 && batteryHealth < 40)
- return R.drawable.stat_sys_battery_charge_anim07;
- if (batteryHealth >= 40 && batteryHealth < 45)
- return R.drawable.stat_sys_battery_charge_anim08;
- if (batteryHealth >= 45 && batteryHealth < 50)
- return R.drawable.stat_sys_battery_charge_anim09;
- if (batteryHealth >= 50 && batteryHealth < 55)
- return R.drawable.stat_sys_battery_charge_anim10;
- if (batteryHealth >= 55 && batteryHealth < 60)
- return R.drawable.stat_sys_battery_charge_anim11;
- if (batteryHealth >= 60 && batteryHealth < 65)
- return R.drawable.stat_sys_battery_charge_anim12;
- if (batteryHealth >= 65 && batteryHealth < 70)
- return R.drawable.stat_sys_battery_charge_anim13;
- if (batteryHealth >= 70 && batteryHealth < 75)
- return R.drawable.stat_sys_battery_charge_anim14;
- if (batteryHealth >= 75 && batteryHealth < 80)
- return R.drawable.stat_sys_battery_charge_anim15;
- if (batteryHealth >= 80 && batteryHealth < 85)
- return R.drawable.stat_sys_battery_charge_anim16;
- if (batteryHealth >= 85 && batteryHealth < 90)
- return R.drawable.stat_sys_battery_charge_anim17;
- if (batteryHealth >= 90 && batteryHealth < 95)
- return R.drawable.stat_sys_battery_charge_anim18;
- if (batteryHealth >= 95 && batteryHealth < 100)
- return R.drawable.stat_sys_battery_charge_anim19;
- if (batteryHealth == 100)
- return R.drawable.stat_sys_battery_charge_animfull;
- return R.drawable.stat_sys_battery_unknown;
- }
-
- // -------------------------------私有字段--------------------------------------
- private NotificationManager notifyManager = null;
- private Notification notification = null;
-
- //這裡是為了方便起見而硬編碼的icon資源數組,直接從R.java文件中提取的十六進制資源ID,本數組的0號元素0x7f020002表示資源stat_sys_battery_0.png(見附件),以後累加。不同配置的ID可能不同,請讀者自行修改。
- private int batteryStateIcons[] = { 0x7f020002, 0x7f020003, 0x7f020004,
- 0x7f020005, 0x7f020006, 0x7f020007, 0x7f020008, 0x7f020009, 0x7f02000a,
- 0x7f02000b, 0x7f02000c, 0x7f02000d, 0x7f02000e, 0x7f02000f, 0x7f020010,
- 0x7f020011, 0x7f020012, 0x7f020013, 0x7f020014, 0x7f020015, 0x7f020016,
- 0x7f020017, 0x7f020018, 0x7f020019, 0x7f02001a, 0x7f02001b, 0x7f02001c,
- 0x7f02001d, 0x7f02001e, 0x7f02001f, 0x7f020020, 0x7f020021, 0x7f020022,
- 0x7f020023, 0x7f020024, 0x7f020025, 0x7f020026, 0x7f020027, 0x7f020028,
- 0x7f020029, 0x7f02002a, 0x7f02002b, 0x7f02002c, 0x7f02002d, 0x7f02002e,
- 0x7f02002f, 0x7f020030, 0x7f020031, 0x7f020032, 0x7f020033, 0x7f020034,
- 0x7f020035, 0x7f020036, 0x7f020037, 0x7f020038, 0x7f020039, 0x7f02003a,
- 0x7f02003b, 0x7f02003c, 0x7f02003d, 0x7f02003e, 0x7f02003f, 0x7f020040,
- 0x7f020041, 0x7f020042, 0x7f020043, 0x7f020044, 0x7f020045, 0x7f020046,
- 0x7f020047, 0x7f020048, 0x7f020049, 0x7f02004a, 0x7f02004b, 0x7f02004c,
- 0x7f02004d, 0x7f02004e, 0x7f02004f, 0x7f020050, 0x7f020051, 0x7f020052,
- 0x7f020053, 0x7f020054, 0x7f020055, 0x7f020056, 0x7f020057, 0x7f020058,
- 0x7f020059, 0x7f02005a, 0x7f02005b, 0x7f02005c, 0x7f02005d, 0x7f02005e,
- 0x7f02005f, 0x7f020060, 0x7f020061, 0x7f020062, 0x7f020063, 0x7f020064,
- 0x7f020065, 0x7f02007b };
-
- // 接受電池信息更新的廣播
- private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- int level = intent.getIntExtra("level", 0);
- int scale = intent.getIntExtra("scale", 100);
- int status = intent.getIntExtra("status", 0);
-
- // 若正在充電
- if (status == BatteryManager.BATTERY_STATUS_CHARGING)
- notification = getNotification(getChargingIcon(level * 100 / scale),
- "Battery Monitor", System.currentTimeMillis(), "電池電量", "正在充電");
- else
- notification = getNotification(getStateIcon(level * 100 / scale),
- "Battery Monitor", System.currentTimeMillis(), "電池電量", "請及時充電");
- notifyManager.notify(0, notification);
- }
- };
- }