Android - 電池狀態

來源:互聯網
上載者:User

標籤:

為瞭解決電池表徵圖的問題,順帶看了看電池資訊的擷取方法 ;自己寫了一個小栗子,來驗證一下效果

電池的資訊,一般都在BatteryManager裡面,資訊是用廣播發出的。我們更新資訊需要一個廣播接收器

註冊一個廣播接收器,接收  Intent.ACTION_BATTERY_CHANGED ,從intent中讀出想要的電池資訊

比如 BatteryManager.EXTRA_STATUS     BatteryManager.BATTERY_STATUS_CHARGING  等等

在Android 5.1中,電池資訊可以在Settings - Battery 裡面找到

 1 package com.example.chargingwatching; 2  3 import android.app.Activity; 4 import android.content.BroadcastReceiver; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.IntentFilter; 8 import android.os.BatteryManager; 9 import android.os.Bundle;10 import android.view.WindowManager;11 import android.widget.TextView;12 13 public class MainActivity extends Activity {14     private TextView chargeStatus;15     private TextView LevelDigit;16     private TextView percentView;17     private int rustLevel = 0;    /* battery level */18     private int windowWidth = 0;    19     private WindowManager mWindowManager;20 21     private BroadcastReceiver mBatteryStatuReceiver = new BroadcastReceiver() {22         int status;    // current battery status23         int plugType; 24         public void onReceive(Context context, Intent intent)    {25 26             rustLevel = (int)(100f 27                     * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)28                     / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));29 30             LevelDigit.setText("  " + rustLevel + "%");31             32             percentView.setWidth((int)(windowWidth * rustLevel /100));     33             status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,34                     BatteryManager.BATTERY_STATUS_UNKNOWN);35             plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);36 37             if(status == BatteryManager.BATTERY_STATUS_CHARGING) {38                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {39                     chargeStatus.setText(R.string.ac_charging);40                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {41                     chargeStatus.setText(R.string.usb_charging);42                 }43             } else if (status == BatteryManager.BATTERY_STATUS_FULL) {44                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {45                     chargeStatus.setText(R.string.full_ac);46                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {47                     chargeStatus.setText(R.string.full_usb);48                 }49             } else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {50                 chargeStatus.setText(R.string.uncharge);51             } else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING) {52                 chargeStatus.setText(R.string.discharging);53             } else {54                 chargeStatus.setText(R.string.unknown);55             }56         }57     };58 59     @SuppressWarnings("deprecation")60     @Override61     public void onCreate(Bundle savedInstanceState) {62         super.onCreate(savedInstanceState);63         setContentView(R.layout.watch_charging);64         chargeStatus = (TextView)findViewById(R.id.tv_charge);65         LevelDigit = (TextView) findViewById(R.id.tv_battery_level_digit);66         percentView = (TextView) findViewById(R.id.percent_view);67 68         mWindowManager = this.getWindowManager();69         windowWidth = mWindowManager.getDefaultDisplay().getWidth();70 71         IntentFilter filter = new IntentFilter();    72         filter.addAction(Intent.ACTION_BATTERY_CHANGED);73         registerReceiver(mBatteryStatuReceiver, filter);74     }75 }

以下是簡單的布局檔案

主體是LinearLayout,放一個TextView來顯示電池狀態,一個TextView來顯示電量百分比

一個簡陋的電量條,拿TextView來冒充的

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:id="@+id/tv_charge" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         android:text="Detecting..." 12         android:textSize="20sp"13         />14 15     <LinearLayout16         android:layout_width="match_parent"17         android:layout_height="wrap_content"18         android:orientation="horizontal" >19 20         <TextView21             android:id="@+id/tv_battery_level"22             android:layout_width="wrap_content"23             android:layout_height="match_parent"24             android:text="@string/battery_level" 25             android:textSize="20sp"/>26 27         <TextView28             android:id="@+id/tv_battery_level_digit"29             android:layout_width="match_parent"30             android:layout_height="match_parent" 31             android:textSize="20sp"/>32     </LinearLayout>33 34     <TextView 35         android:id="@+id/percent_view"36         android:layout_height="20dp"37         android:layout_width="wrap_content"38         android:background="#00AA00"39         />40 </LinearLayout>

 

Android - 電池狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.