Android 可以通過BroadcastReceiver來獲取電池信息改變的廣播(ACTION_BATTERY_CHANGED),從而獲取到相關的電池信息。
電池信息,及其對應的相關常數(參考網址:http://blog.sina.com.cn/s/blog_5d2e69770102vh59.html)
電池信息 | 類型 | 備注 |
status | int | 取得電池的狀態,返回的狀態類型由 android.os.BatteryManager 類定義的常量所決定,包括: |
電池充電狀態( BATTERY_STATUS_CHARGING ) | ||
電池放電狀態( BATTERY_STATUS_DISCHARGING ) | ||
電池滿電狀態( BATTERY_STATUS_FULL ) | ||
電池不充電狀態( BATTERY_STATUS_NOT_CHARGING ) | ||
電池未知狀態( BATTERY_STATUS_UNKNOWN ) | ||
health | int | 取得電池的健康狀態,返回的狀態類型由 android.os.BatteryManager 類定義的常量所決定,包括: |
電池損壞( BATTERY_HEALTH_DEAD ) | ||
電池健康( BATTERY_HEALTH_GOOD ) | ||
電池過熱( BATTERY_HEALTH_OVERHEAT ) | ||
電池電壓過大( BATTERY_HEALTH_OVER_VOLTAGE ) | ||
未知狀態( BATTERY_HEALTH_UNKOWN ) | ||
未明示故障( BATTERY_HEALTH_UNSPECIFIED_FAILURE ) | ||
present | boolean | 判斷當前是否存在電池 |
level | int | 取得電池的剩余容量 |
scale | int | 取得電池的總容量,通常為 100 |
Icon-small | int | 取得電池對應的圖標 ID |
plugged | int | 連接的電源插座類型,返回的狀態由 android.os.BatteryManager 類定義的常量所決定,包括: |
USB 電源( BATTERY_PLUGGED_USB ) | ||
交流電電源( BATTERY_PLUGGED_AC ) | ||
voltage | int | 取得電池的電壓 |
temperature | int | 取得電池的溫度,單位是攝氏度 |
technology | String | 取得電池的類型 |
獲取電池信息的步驟:
(1) 創建BroadcastReceiver,用於結束電池信息變化的廣播
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { //TO GET BatteryInfo }
(2) 重寫onResume()方法,獲取IntentFilter對象,並注冊BroadcastReceiver
protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mBroadcastReceiver, filter); super.onResume(); }
(3)程序在后台的時候取消注冊BroadcastReceiver
protected void onPause() { unregisterReceiver(mBroadcastReceiver); super.onPause(); }
實例:
public class GetBatteryInfo extends Activity { private static final int REFRESH_TIME = 1; private static boolean runtime=false; private TextView mStatus_BatteryInfo; private TextView mPlugged_BatteryInfo; private TextView mLevel_BatteryInfo; private TextView mScale_BatteryInfo; private TextView mVoltage_BatteryInfo; private TextView mTemperature_BatteryInfo; private TextView mTechnology_BatteryInfo; private TextView mHealth_BatteryInfo; private TextView mRuntime_BatteryInfo; private TimeThread timeThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitView(); } private void InitView() { mStatus_BatteryInfo=(TextView)findViewById(R.id.status_BatteryInfo); mPlugged_BatteryInfo=(TextView)findViewById(R.id.plugged_BatteryInfo); mLevel_BatteryInfo=(TextView)findViewById(R.id.level_BatteryInfo); mScale_BatteryInfo=(TextView)findViewById(R.id.scale_BatteryInfo); mVoltage_BatteryInfo=(TextView)findViewById(R.id.voltage_BatteryInfo); mTemperature_BatteryInfo=(TextView)findViewById(R.id.temperature_BatteryInfo); mTechnology_BatteryInfo=(TextView)findViewById(R.id.technology_BatteryInfo); mHealth_BatteryInfo=(TextView)findViewById(R.id.health_BatteryInfo); mRuntime_BatteryInfo=(TextView)findViewById(R.id.runtime_BatteryInfo); } @Override protected void onResume() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(mBroadcastReceiver, filter); startTimeThread(timeThread); super.onResume(); } @Override protected void onPause() { stopTimeThread(timeThread); unregisterReceiver(mBroadcastReceiver); super.onPause(); } /** * @param milliseconds 將ms轉化為hh:mm:ss 格式時間 * @return */ private String msToTime(int milliseconds ){ int allSeconds=(milliseconds/1000); int hours=allSeconds/3600; String hours_string; int feelSeconds=allSeconds%3600; int mimute=feelSeconds/60; String mimute_string; String second_string; int second=feelSeconds%60; if(hours<10){ hours_string="0"+String.valueOf(hours); }else{ hours_string=String.valueOf(hours); } if(mimute<10){ mimute_string="0"+String.valueOf(mimute); }else{ mimute_string=String.valueOf(mimute); } if(second<10){ second_string="0"+String.valueOf(second); }else{ second_string=String.valueOf(second); } return hours_string+":"+mimute_string+":"+second_string; } private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { int status = intent.getIntExtra("status", 0); int health = intent.getIntExtra("health", 0); int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 0); int plugged = intent.getIntExtra("plugged", 0); int voltage = intent.getIntExtra("voltage", 0); int temperature = intent.getIntExtra("temperature", 0); String technology = intent.getStringExtra("technology"); mLevel_BatteryInfo.setText(level+""); mScale_BatteryInfo.setText(scale+""); mVoltage_BatteryInfo.setText(voltage+"mV"); mTemperature_BatteryInfo.setText(((float)temperature/10)+"℃"); mTechnology_BatteryInfo.setText(technology+""); mRuntime_BatteryInfo.setText(msToTime((int) SystemClock.elapsedRealtime())); switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_UNKNOWN)); break; case BatteryManager.BATTERY_STATUS_CHARGING: mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_CHARGING)); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_DISCHARGING)); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_NOT_CHARGING)); break; case BatteryManager.BATTERY_STATUS_FULL: mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_FULL)); break; } switch (health) { case BatteryManager.BATTERY_HEALTH_UNKNOWN: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_UNKOWN)); break; case BatteryManager.BATTERY_HEALTH_GOOD: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_GOOD)); break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_OVERHEAT)); break; case BatteryManager.BATTERY_HEALTH_DEAD: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_DEAD)); break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_VOLTAGE)); break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_UNSPECIFIED_FAILURE)); break; } switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: mPlugged_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_plugged_AC)); break; case BatteryManager.BATTERY_PLUGGED_USB: mPlugged_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_plugged_USB)); break; default: mPlugged_BatteryInfo.setText(""); } } } }; Handler TimeHandler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case REFRESH_TIME: mRuntime_BatteryInfo.setText(msToTime((int) SystemClock.elapsedRealtime()));//獲取系統啟動時間 break; default: break; } super.handleMessage(msg); } }; /** * 打開線程timeThread * @param timeThread * @param runtime 運行標志位 */ private void startTimeThread(Thread timeThread){ if(timeThread==null){ runtime=true; timeThread=new TimeThread(); timeThread.start(); } } /** * 停止線程 * @param timeThread */ private void stopTimeThread(Thread timeThread){ if(timeThread!=null){ Thread.currentThread().interrupt(); timeThread.interrupt(); runtime=false; } } class TimeThread extends Thread{ @Override public void run() { while(runtime){ try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Message msg=new Message(); msg.what=REFRESH_TIME; TimeHandler.sendMessage(msg); } } } }
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:clipToPadding="true" android:fitsSystemWindows="true" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="#5CD8AF" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="@string/SystemSetBatteryInfo_BatteryInfo_textvie" android:textColor="@android:color/white" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_status_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/status_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_plugged_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/plugged_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_level_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/level_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_scale_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/scale_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_voltage_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/voltage_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_temperature_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/temperature_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_technology_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/technology_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_health_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/health_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#FFFFFF" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="@string/SystemSetBatteryInfo_runtime_textview" android:textColor="@color/main_view_color" android:textSize="18sp" /> <TextView android:id="@+id/runtime_BatteryInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/main_view_color" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">GetBatteryInfo</string> <string name="SystemSetBatteryInfo_BatteryInfo_textvie">電池信息</string> <string name="SystemSetBatteryInfo_status_textview">電池狀態:</string> <string name="SystemSetBatteryInfo_plugged_textview">充電方式:</string> <string name="SystemSetBatteryInfo_level_textview">電池電量:</string> <string name="SystemSetBatteryInfo_scale_textview">電池容量:</string> <string name="SystemSetBatteryInfo_voltage_textview">電池電壓:</string> <string name="SystemSetBatteryInfo_temperature_textview">電池溫度:</string> <string name="SystemSetBatteryInfo_technology_textview">電池類型:</string> <string name="SystemSetBatteryInfo_health_textview">健康狀態:</string> <string name="SystemSetBatteryInfo_runtime_textview">使用時間:</string> <string name="BatteryInfo_status_CHARGING">正在充電</string> <string name="BatteryInfo_status_DISCHARGING">正在耗電</string> <string name="BatteryInfo_status_FULL">電量充滿</string> <string name="BatteryInfo_status_NOT_CHARGING ">未充電</string> <string name="BatteryInfo_status_UNKNOWN">狀態未知</string> <string name="BatteryInfo_plugged_USB">USB電源</string> <string name="BatteryInfo_plugged_AC">交流電電源</string> <string name="BatteryInfo_health_DEAD">電池損壞</string> <string name="BatteryInfo_health_GOOD">電池正常</string> <string name="BatteryInfo_health_OVERHEAT">電池過熱</string> <string name="BatteryInfo_health_VOLTAGE ">電池電壓過大</string> <string name="BatteryInfo_health_UNKOWN">未知狀態</string> <string name="BatteryInfo_health_UNSPECIFIED_FAILURE">未明示故障</string> </resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="main_view_color">#1f1f1f</color> <color name="main_text_color">#6f6f6f</color> </resources>