zoukankan      html  css  js  c++  java
  • 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
                            + "
    健康值: "+ healthString
                            + "
    电池剩余容量: " + level
                            + "
    电池的最大值:" + scale
                            + "
    小图标:" + icon_small
                            + "
    充电方式:" + plugged
                            + "
    充电方式: " + acString
                            + "
    电池的电压:" + voltage
                            + "
    电池的温度:" + (float) temperature * 0.1
                            + "
    电池的类型:" + technology);
                }
            }
        };
    
        @Override
        protected void onPause() {
            super.onPause();
            // 解除注册监听
            unregisterReceiver(mBroadcastReceiver);
        }
    }
  • 相关阅读:
    在数值中加入千位分隔符的方法
    用 Javascript 验证表单(form)中的单选(radio)值
    用 Javascript 验证表单(form)中多选框(checkbox)值
    用 CSS 实现图片替换文字(Image replacement)
    计算机技术分类
    最近好乱acm与数模时间重复了
    memcached Telnet Interface
    event_new
    event_base_loop
    event_base_loop
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5227979.html
Copyright © 2011-2022 走看看