andriod 擷取電池的資訊,andriod擷取電池

來源:互聯網
上載者:User

andriod 擷取電池的資訊,andriod擷取電池

<?xml version="1.0"?>    <LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">    <Button android:layout_height="wrap_content" android:layout_width="match_parent" android:text="擷取電池的資訊" android:id="@+id/btn_battery"/>    <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/tv_battery"/></LinearLayout>
package com.example.yanlei.wifi;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.support.v7.app.AppCompatActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    // 定義電池資訊的按鈕    private Button btnBattery;    // 定義顯示電池資訊的textview    private TextView tvBattery;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 得到布局中的所有對象        findView();        // 設定對象的監聽器        setListener();    }    private void findView() {        // 得到布局中的所有對象        btnBattery = (Button) findViewById(R.id.btn_battery);        tvBattery = (TextView) findViewById(R.id.tv_battery);    }    // 設定對象的監聽器    private void setListener() {        btnBattery.setOnClickListener(listener);    }    OnClickListener listener = new OnClickListener() {        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            switch (v.getId()) {                // 當前的音量                case R.id.btn_battery:                    IntentFilter filter = new IntentFilter();                    filter.addAction(Intent.ACTION_BATTERY_CHANGED);                    registerReceiver(mBroadcastReceiver, filter);                    break;            }        }    };    // 聲明廣播接受者對象    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            // TODO Auto-generated method stub            String action = intent.getAction();            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {                // 得到電池狀態:                // BatteryManager.BATTERY_STATUS_CHARGING:充電狀態。                // BatteryManager.BATTERY_STATUS_DISCHARGING:放電狀態。                // BatteryManager.BATTERY_STATUS_NOT_CHARGING:未充滿。                // BatteryManager.BATTERY_STATUS_FULL:充滿電。                // BatteryManager.BATTERY_STATUS_UNKNOWN:未知狀態。                int status = intent.getIntExtra("status", 0);                // 得到健康狀態:                // BatteryManager.BATTERY_HEALTH_GOOD:狀態良好。                // BatteryManager.BATTERY_HEALTH_DEAD:電池沒有電。                // BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:電池電壓過高。                // BatteryManager.BATTERY_HEALTH_OVERHEAT:電池過熱。                // BatteryManager.BATTERY_HEALTH_UNKNOWN:未知狀態。                int health = intent.getIntExtra("health", 0);                // boolean類型                boolean present = intent.getBooleanExtra("present", false);                // 得到電池剩餘容量                int level = intent.getIntExtra("level", 0);                // 得到電池最大值。通常為100。                int scale = intent.getIntExtra("scale", 0);                // 得到表徵圖ID                int icon_small = intent.getIntExtra("icon-small", 0);                // 充電方式: BatteryManager.BATTERY_PLUGGED_AC:AC充電。 BatteryManager.BATTERY_PLUGGED_USB:USB充電。                int plugged = intent.getIntExtra("plugged", 0);                // 得到電池的電壓                int voltage = intent.getIntExtra("voltage", 0);                // 得到電池的溫度,0.1度單位。例如 表示197的時候,意思為19.7度                int temperature = intent.getIntExtra("temperature", 0);                // 得到電池的類型                String technology = intent.getStringExtra("technology");                // 得到電池狀態                String statusString = "";                // 根據狀態id,得到狀態字串                switch (status) {                    case BatteryManager.BATTERY_STATUS_UNKNOWN:                        statusString = "unknown";                        break;                    case BatteryManager.BATTERY_STATUS_CHARGING:                        statusString = "charging";                        break;                    case BatteryManager.BATTERY_STATUS_DISCHARGING:                        statusString = "discharging";                        break;                    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:                        statusString = "not charging";                        break;                    case BatteryManager.BATTERY_STATUS_FULL:                        statusString = "full";                        break;                }                //得到電池的壽命狀態                String healthString = "";                //根據狀態id,得到電池壽命                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 = "";                //根據充電狀態id,得到充電模式                switch (plugged) {                    case BatteryManager.BATTERY_PLUGGED_AC:                        acString = "plugged ac";                        break;                    case BatteryManager.BATTERY_PLUGGED_USB:                        acString = "plugged usb";                        break;                }                //顯示電池資訊                tvBattery.setText("電池的狀態:" + statusString                        + "\n健康值: "+ healthString                        + "\n電池剩餘容量: " + level                        + "\n電池的最大值:" + scale                        + "\n小表徵圖:" + icon_small                        + "\n充電方式:" + plugged                        + "\n充電方式: " + acString                        + "\n電池的電壓:" + voltage                        + "\n電池的溫度:" + (float) temperature * 0.1                        + "\n電池的類型:" + technology);            }        }    };    @Override    protected void onPause() {        super.onPause();        // 解除註冊監聽        unregisterReceiver(mBroadcastReceiver);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.