編輯:Android開發實例
本文實例講述了Android實現偵聽電池狀態顯示、電量及充電動態顯示的方法,是Android應用程序開發中非常常用的重要功能。分享給大家供大家參考之用。具體方法如下:
使用android系統的電池偵聽和系統的電池圖片如下;
引入源代碼中的兩個xml文件(這裡我在android2.2的jar包中找不到,所以才引入,源代碼路徑:C:\android-sdk-windows\platforms\android-8\data\res\drawable),之後會發現stat_sys_battery.xml報錯,如下圖所示:
1.這些圖引入到我的drawable目錄下,然後將stat_sys_battery.xml中的圖片引入路徑更改,如下:
<level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:maxLevel="4" android:drawable="@drawable/stat_sys_battery_0" /> <item android:maxLevel="14" android:drawable="@drawable/stat_sys_battery_10" /> <item android:maxLevel="29" android:drawable="@drawable/stat_sys_battery_20" /> <item android:maxLevel="49" android:drawable="@drawable/stat_sys_battery_40" /> <item android:maxLevel="69" android:drawable="@drawable/stat_sys_battery_60" /> <item android:maxLevel="89" android:drawable="@drawable/stat_sys_battery_80" /> <item android:maxLevel="100" android:drawable="@drawable/stat_sys_battery_100" /> </level-list>
2.接下來是在activity中的操作:
創建一個IntentFilter
private IntentFilter mIntentFilter;
在oncreate的方法中:加入:
//電池相關 mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
3.接收廣播
//聲明消息處理過程 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); int status = intent.getIntExtra("status", 0); int health = intent.getIntExtra("health", 0); boolean present = intent.getBooleanExtra("present", false); int level = intent.getIntExtra("level", 0); int scale = intent.getIntExtra("scale", 0); int icon_small = intent.getIntExtra("icon-small", 0); int plugged = intent.getIntExtra("plugged", 0); int voltage = intent.getIntExtra("voltage", 0); int temperature = intent.getIntExtra("temperature", 0); String technology = intent.getStringExtra("technology"); String statusString = ""; switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: statusString = "unknown"; break; case BatteryManager.BATTERY_STATUS_CHARGING: statusString = "charging"; battery_image.setImageResource(R.drawable.stat_sys_battery_charge); battery_image.getDrawable().setLevel(level); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusString = "discharging"; battery_image.setImageResource(R.drawable.stat_sys_battery); battery_image.getDrawable().setLevel(level); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusString = "not charging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusString = "full"; break; } String healthString = ""; switch (health) { case BatteryManager.BATTERY_HEALTH_UNKNOWN: healthString = "unknown"; break; case BatteryManager.BATTERY_HEALTH_GOOD: healthString = "good"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: healthString = "overheat"; break; case BatteryManager.BATTERY_HEALTH_DEAD: healthString = "dead"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: healthString = "voltage"; break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: healthString = "unspecified failure"; break; } String acString = ""; switch (plugged) { case BatteryManager.BATTERY_PLUGGED_AC: acString = "plugged ac"; break; case BatteryManager.BATTERY_PLUGGED_USB: acString = "plugged usb"; break; } Log.i("cat", statusString); Log.i("cat", healthString); Log.i("cat", String.valueOf(present)); Log.i("cat", String.valueOf(level)); Log.i("cat", String.valueOf(scale)); Log.i("cat", String.valueOf(icon_small)); Log.i("cat", acString); Log.i("cat", String.valueOf(voltage)); Log.i("cat", String.valueOf(temperature)); Log.i("cat", technology); //要看看是不是我們要處理的消息 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { //電池電量,數字 Log.d("Battery", "" + intent.getIntExtra("level", 0)); //電池最大容量 Log.d("Battery", "" + intent.getIntExtra("scale", 0)); //電池伏數 Log.d("Battery", "" + intent.getIntExtra("voltage", 0)); //電池溫度 Log.d("Battery", "" + intent.getIntExtra("temperature", 0)); //電池狀態,返回是一個數字 // BatteryManager.BATTERY_STATUS_CHARGING 表示是充電狀態 // BatteryManager.BATTERY_STATUS_DISCHARGING 放電中 // BatteryManager.BATTERY_STATUS_NOT_CHARGING 未充電 // BatteryManager.BATTERY_STATUS_FULL 電池滿 Log.d("Battery", "ss" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_CHARGING)); //充電類型 BatteryManager.BATTERY_PLUGGED_AC 表示是充電器,不是這個值,表示是 USB Log.d("Battery", "" + intent.getIntExtra("plugged", 0)); //電池健康情況,返回也是一個數字 //BatteryManager.BATTERY_HEALTH_GOOD 良好 //BatteryManager.BATTERY_HEALTH_OVERHEAT 過熱 //BatteryManager.BATTERY_HEALTH_DEAD 沒電 //BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE 過電壓 //BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE 未知錯誤 Log.d("Battery", "" + intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)); } } };
這裡真正處理圖片變換的是以下幾句話:
switch (status) { case BatteryManager.BATTERY_STATUS_UNKNOWN: statusString = "unknown"; break; case BatteryManager.BATTERY_STATUS_CHARGING: statusString = "charging"; battery_image.setImageResource(R.drawable.stat_sys_battery_charge); battery_image.getDrawable().setLevel(level); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: statusString = "discharging"; battery_image.setImageResource(R.drawable.stat_sys_battery); battery_image.getDrawable().setLevel(level); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: statusString = "not charging"; break; case BatteryManager.BATTERY_STATUS_FULL: statusString = "full"; break; }
4.在onResume方法中注冊消息處理器
registerReceiver(mIntentReceiver, mIntentFilter);
5.在onPause方法中注銷
unregisterReceiver(mIntentReceiver);
希望本文所述對大家的Android程序設計有所幫助。
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
上一節中講了App->Activity->Custom Title
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放