電池的資訊,電壓,溫度,充電狀態等等,都是由BatteryService來提供的。BatteryService是跑在system_process當中,在系統初始化的時候啟動,如下
在SystemServer.java中可以看到啟動BatteryService的代碼:
Log.i(TAG, "Starting Battery Service."); BatteryService battery = new BatteryService(context); ServiceManager.addService("battery", battery);
========================================================================================
1. 資料來源
BatteryService通過JNI(com_android_server_BatteryService.cpp)讀取資料。BatteryService通過JNI註冊的不僅有函數,還有變數。 如下:
//##############在BatteryService.java中聲明的變數################
private boolean mAcOnline; private boolean mUsbOnline; private int mBatteryStatus; private int mBatteryHealth; private boolean mBatteryPresent; private int mBatteryLevel; private int mBatteryVoltage; private int mBatteryTemperature; private String mBatteryTechnology;
//在BatteryService.java中聲明的變數,在com_android_server_BatteryService.cpp中共用,即在com_android_server_BatteryService.cpp中其實操作的也是BatteryService.java中聲明的變數
gFieldIds.mAcOnline = env->GetFieldID(clazz, "mAcOnline", "Z"); gFieldIds.mUsbOnline = env->GetFieldID(clazz, "mUsbOnline", "Z"); gFieldIds.mBatteryStatus = env->GetFieldID(clazz, "mBatteryStatus", "I"); gFieldIds.mBatteryHealth = env->GetFieldID(clazz, "mBatteryHealth", "I"); gFieldIds.mBatteryPresent = env->GetFieldID(clazz, "mBatteryPresent", "Z"); gFieldIds.mBatteryLevel = env->GetFieldID(clazz, "mBatteryLevel", "I"); gFieldIds.mBatteryTechnology = env->GetFieldID(clazz, "mBatteryTechnology", "Ljava/lang/String;"); gFieldIds.mBatteryVoltage = env->GetFieldID(clazz, "mBatteryVoltage", "I"); gFieldIds.mBatteryTemperature = env->GetFieldID(clazz, "mBatteryTemperature", "I");
//上面這些變數的值,對應是從下面的檔案中讀取的,一隻檔案儲存體一個數值。
#define AC_ONLINE_PATH "/sys/class/power_supply/ac/online" #define USB_ONLINE_PATH "/sys/class/power_supply/usb/online" #define BATTERY_STATUS_PATH "/sys/class/power_supply/battery/status" #define BATTERY_HEALTH_PATH "/sys/class/power_supply/battery/health" #define BATTERY_PRESENT_PATH "/sys/class/power_supply/battery/present" #define BATTERY_CAPACITY_PATH "/sys/class/power_supply/battery/capacity" #define BATTERY_VOLTAGE_PATH "/sys/class/power_supply/battery/batt_vol" #define BATTERY_TEMPERATURE_PATH "/sys/class/power_supply/battery/batt_temp" #define BATTERY_TECHNOLOGY_PATH "/sys/class/power_supply/battery/technology"
Android是運行在Linux核心上面的,/sys/class/power_supply亦是Linux核心下面的目錄。至於這些檔案時怎麼產生的,則是由Platform來控制的。
-----------------------------------------------------------------------------------------------------------------------------------
2. 資料傳送
電池的這些資訊是通過何種方式,被其他應用所獲得的。可以想到的有兩種方式,第一種,應用主動從BatteryService獲得資料;第二種,BatteryService主動把資料傳送給所關心的應用程式。
BatteryService採用的是第二種方式,所有的電池的資訊資料是通過Intent傳送出去的。在BatteryService.java中,Code如下:
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); intent.putExtra("status", mBatteryStatus); intent.putExtra("health", mBatteryHealth); intent.putExtra("present", mBatteryPresent); intent.putExtra("level", mBatteryLevel); intent.putExtra("scale", BATTERY_SCALE); intent.putExtra("icon-small", icon); intent.putExtra("plugged", mPlugType); intent.putExtra("voltage", mBatteryVoltage); intent.putExtra("temperature", mBatteryTemperature); intent.putExtra("technology", mBatteryTechnology); ActivityManagerNative.broadcastStickyIntent(intent, null);
-----------------------------------------------------------------------------------------------------------------------------------
3. 資料接收
應用如果想要接收到BatteryService發送出來的電池資訊,則需要註冊一個Intent為Intent.ACTION_BATTERY_CHANGED的BroadcastReceiver。
註冊方法如下:
IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mIntentReceiver, mIntentFilter); private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { int nVoltage = intent.getIntExtra("voltage", 0); if(nVoltage!=0){ mVoltage.setText("V: " + nVoltage + "mV - Success..."); } else{ mVoltage.setText("V: " + nVoltage + "mV - fail..."); } } } };
-----------------------------------------------------------------------------------------------------------------------------------
4. 資料更新
電池的資訊會隨著時間不停變化,自然地,就需要考慮如何即時的更新電池的資料資訊。在BatteryService啟動的時候,會同時通過UEventObserver啟動一個onUEvent Thread。
每一個Process最多隻能有一個onUEvent Thread,即使這個Process中有多個UEventObserver的執行個體。當在一個Process中,第一次Call startObserving()方法後,這個UEvent thread就啟動了。
而一旦這個UEvent thread啟動之後,就不會停止。
//在BatteryService.java中
mUEventObserver.startObserving("SUBSYSTEM=power_supply"); private UEventObserver mUEventObserver = new UEventObserver() { @Override public void onUEvent(UEventObserver.UEvent event) { update(); } };
在UEvent thread中會不停調用 update()方法,來更新電池的資訊資料。
原文地址:http://hi.baidu.com/maoxiaofei_2009/blog/item/39f147894b162518c8fc7a97.html