Android實時獲取電量
阿新 • • 發佈:2019-02-14
Android開發者可以利用BroadcastReceiver機制,獲取電池電量變化的ACTION_BATTERY_CHANGED intent,進而獲取當前android裝置的電池狀態。
具體操作步驟:
1. 建立一個監聽ACTION_BATTERY_CHANGED事件的intentFilter。
2. 建立一個BroadcastReceiver物件,該物件可以接收broadcast intent。
3. 註冊BroadcastReceiver物件來監聽ACTION_BATTERY_CHANGED事件。
4. 在BroadcastReceiver物件中,重寫onReceive方法,在onReceive方法的傳入引數intent裡獲取需要的電池狀態資訊。
5. 在生命週期結束時,取消step4中的註冊。
- publicclass BatteryMonitorActivity extends Activity {
- private TextView batterLevel;
- private BroadcastReceiver batteryLevelRcvr;
- private IntentFilter batteryLevelFilter;
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
-
super
- setContentView(R.layout.main);
- batterLevel = (TextView) findViewById(R.id.batteryLevel);
- monitorBatteryState();
- }
- @Override
- protectedvoid onDestroy() {
- super.onDestroy();
-
unregisterReceiver(batteryLevelRcvr);
- }
- privatevoid monitorBatteryState() {
- batteryLevelRcvr = new BroadcastReceiver() {
- publicvoid onReceive(Context context, Intent intent) {
- StringBuilder sb = new StringBuilder();
- int rawlevel = intent.getIntExtra("level", -1);
- int scale = intent.getIntExtra("scale", -1);
- int status = intent.getIntExtra("status", -1);
- int health = intent.getIntExtra("health", -1);
- int level = -1; // percentage, or -1 for unknown
- if (rawlevel >= 0 && scale > 0) {
- level = (rawlevel * 100) / scale;
- }
- sb.append("The phone");
- if (BatteryManager.BATTERY_HEALTH_OVERHEAT == health) {
- sb.append("'s battery feels very hot!");
- } else {
- switch (status) {
- case BatteryManager.BATTERY_STATUS_UNKNOWN:
- sb.append("no battery.");
- break;
- case BatteryManager.BATTERY_STATUS_CHARGING:
- sb.append("'s battery");
- if (level <= 33)
- sb.append(" is charging, battery level is low"
- + "[" + level + "]");
- elseif (level <= 84)
- sb.append(" is charging." + "[" + level + "]");
- else
- sb.append(" will be fully charged.");
- break;
- case BatteryManager.BATTERY_STATUS_DISCHARGING:
- case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
- if (level == 0)
- sb.append(" needs charging right away.");
- elseif (level > 0 && level <= 33)
- sb.append(" is about ready to be recharged, battery level is low"
- + "[" + level + "]");
- else
- sb.append("'s battery level is" + "[" + level + "]");
- break;
- case BatteryManager.BATTERY_STATUS_FULL:
- sb.append(" is fully charged.");
- break;
- default:
- sb.append("'s battery is indescribable!");
- break;
- }
- }
- sb.append(' ');
- batterLevel.setText(sb.toString());
- }
- };
- batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- registerReceiver(batteryLevelRcvr, batteryLevelFilter);
- }
- }
[html]view plaincopyprint?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/batteryLevel"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center_vertical|center_horizontal"
- android:textSize="50dip">
- </TextView>
- </LinearLayout>
Android的上層實現顯示手機電池電量是通過監聽實現,當電池電量改變時會通知到程式。
當然這個有朋友就要問了我不是想監聽電池電量,我是想直接獲取當前的電池電量。
其實這是同一個問題,當你在初始化監聽器的時候他會自動首先獲取電池的電量資訊,然後後續當電池改變時同時也會通知當前電池電量的改變情況。
轉自
記得加許可權