Andriod 获取电池的信息实例代码

Andriod 获取电池的信息实例代码,第1张

概述具体代码如下所示:<?xmlversion=\"1.0\"?><LinearLayoutandroid:orientation=\"vertical\"android:layout_height=\"match_parent\"android:layout_width=\"match_parent\"xmlns:tools=\"http:

具体代码如下所示:

<?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;// 定义显示电池信息的textvIEwprivate TextVIEw tvBattery;@OverrIDepublic 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() {@OverrIDepublic voID onClick(VIEw v) {// Todo auto-generated method stubswitch (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() {@OverrIDepublic voID onReceive(Context context,Intent intent) {// Todo auto-generated method stubString 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);// 得到图标IDint 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);}}};@OverrIDeprotected voID onPause() {super.onPause();// 解除注册监听unregisterReceiver(mbroadcastReceiver);}}

以上所述是小编给大家介绍的Andriod 获取电池的信息实例代码,希望对大家有所帮助!

总结

以上是内存溢出为你收集整理的Andriod 获取电池的信息实例代码全部内容,希望文章能够帮你解决Andriod 获取电池的信息实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149643.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存