Android編程實現檢測當前電源狀態的方法

來源:互聯網
上載者:User

   本文執行個體講述了Android編程實現檢測當前電源狀態的方法。分享給大家供大家參考,具體如下:

  檢測到現在在電源狀態:

  IntentFilter mIntentFilter = new IntentFilter();

  mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);

  registerReceiver(mIntentReceiver, mIntentFilter);

  //聲明訊息處理過程

  private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

  @Override

  public void onReceive(Context context, Intent intent) {

  String action = intent.getAction();

  //要看看是不是我們要處理的訊息

  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", "" + intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN));

  //充電類型 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));

  }

  }

  };

  希望本文所述對大家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.