編輯:關於Android編程
如果要監聽電池的狀態改變,需要動態注冊:android.intent.action.BATTERY_CHANGED,收到Action後可以根據對應的Key獲取你需要的信息,更詳細信息可以參考以下例子中的BatteryChangedReceiver類
具體代碼如下所示:
package com.example.charginganimation; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private CustomClipLoading ccl; private BatteryChangedReceiver receiver = new BatteryChangedReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); registerReceiver(receiver, getFilter()); } private IntentFilter getFilter() { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_BATTERY_CHANGED); filter.addAction(Intent.ACTION_BATTERY_LOW); filter.addAction(Intent.ACTION_BATTERY_OKAY); return filter; } @Override protected void onDestroy() { // TODO Auto-generated method stub unregisterReceiver(receiver); super.onDestroy(); } class BatteryChangedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub final String action = intent.getAction(); if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) { System.out .println("BatteryChangedReceiver BATTERY_CHANGED_ACTION---"); // 當前電池的電壓 int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // 電池的健康狀態 int health = intent .getIntExtra(BatteryManager.EXTRA_HEALTH, -1); switch (health) { case BatteryManager.BATTERY_HEALTH_COLD: System.out.println("BATTERY_HEALTH_COLD"); break; case BatteryManager.BATTERY_HEALTH_DEAD: System.out.println("BATTERY_HEALTH_DEAD "); break; case BatteryManager.BATTERY_HEALTH_GOOD: System.out.println("BATTERY_HEALTH_GOOD"); break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: System.out.println("BATTERY_HEALTH_OVERHEAT"); break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: System.out.println("BATTERY_HEALTH_COLD"); break; case BatteryManager.BATTERY_HEALTH_UNKNOWN: System.out.println("BATTERY_HEALTH_UNKNOWN"); break; case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: System.out.println("BATTERY_HEALTH_UNSPECIFIED_FAILURE"); break; default: break; } // 電池當前的電量, 它介於0和 EXTRA_SCALE之間 int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); // 電池電量的最大值 int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); // 當前手機使用的是哪裡的電源 int pluged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); switch (pluged) { case BatteryManager.BATTERY_PLUGGED_AC: // 電源是AC charger.[應該是指充電器] System.out.println("BATTERY_PLUGGED_AC"); break; case BatteryManager.BATTERY_PLUGGED_USB: // 電源是USB port System.out.println("BATTERY_PLUGGED_USB "); break; default: break; } int status = intent .getIntExtra(BatteryManager.EXTRA_STATUS, -1); switch (status) { case BatteryManager.BATTERY_STATUS_CHARGING: // 正在充電 System.out.println("BATTERY_STATUS_CHARGING "); break; case BatteryManager.BATTERY_STATUS_DISCHARGING: System.out.println("BATTERY_STATUS_DISCHARGING "); break; case BatteryManager.BATTERY_STATUS_FULL: // 充滿 System.out.println("BATTERY_STATUS_FULL "); break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: // 沒有充電 System.out.println("BATTERY_STATUS_NOT_CHARGING "); break; case BatteryManager.BATTERY_STATUS_UNKNOWN: // 未知狀態 System.out.println("BATTERY_STATUS_UNKNOWN "); break; default: break; } // 電池使用的技術。比如,對於锂電池是Li-ion String technology = intent .getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); // 當前電池的溫度 int temperature = intent.getIntExtra( BatteryManager.EXTRA_TEMPERATURE, -1); System.out.println("voltage = " + voltage + " technology = " + technology + " temperature = " + temperature + " level = " + level + " scale = " + scale); } else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)) { // 表示當前電池電量低 System.out .println("BatteryChangedReceiver ACTION_BATTERY_LOW---"); } else if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) { // 表示當前電池已經從電量低恢復為正常 System.out .println("BatteryChangedReceiver ACTION_BATTERY_OKAY---"); } } } }
獲取apk和版本信息
private int getVerCode(Context context) { int verCode = -1; try { verCode = context.getPackageManager().getPackageInfo( "com.example.charginganimation", 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return verCode; } private String getVerName(Context context) { String verName = ""; try { verName = context.getPackageManager().getPackageInfo( "com.example.charginganimation", 0).versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return verName; }
以上代碼內容是小編給大家介紹的android監聽電池狀態的實例代碼,本文寫的不好還請見諒,有好的意見歡迎給小編留言,我會及時給大家溝通的,謝謝大家一直以來對本站網站的支持!
可伸縮的矢量圖片不會丟失清晰度,並且單一顏色的app-icon是完美的可定義一個bitmap作為透明度(alpha)和運行時的顏色可對一個bitmap image取色,會
折騰一下午,遇到若干問題:如,設置水平HORIZONTAL時,占用多個跨度(類似單元格的合並)終於都好用了。。。簡陋圖發6張。LinearLayoutManager.VE
本章主要會講述如何在手機端使用HTTP協議和服務器端進行網絡交互,並對服務器返回的數據進行解析,這也是Android中最常使用到的網絡技術了,下面就讓我們一起來學習一下吧
本文為大家分享了Android bitmap使用細節,供大家參考,具體內容如下1、計算機表示圖形的幾種方式1)BMP :幾乎不進行壓縮 占用空間比較大 2)JPG : 在