获取移动设备的电池信息

通过BatteryManager来获取关于电池的信息
实例

package com.example.softwarepatentdemo;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.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class BatteryInfo extends AppCompatActivity {private final String TAG = "BatteryInfoStatus";private double batteryLevel;private double batteryVoltage;private String batteryHealth;private String batteryPlugged;private String batteryPresent;private String batteryStatus;private String batteryTechnology;private double batteryTemperature;private TextView batteryLevelContent,batteryVoltageContent,batteryHealthContent,batteryPluggedContent,batteryPresentContent,batteryStatusContent,batteryTechnologyContent,batteryTemperatureContent;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.battery_info);}@Overrideprotected void onResume() {super.onResume();initView();IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);registerReceiver(batteryLevelReceiver, intentFilter);}public void initView(){batteryLevelContent = findViewById(R.id.batteryLevel);batteryVoltageContent = findViewById(R.id.batteryVoltage);batteryHealthContent = findViewById(R.id.batteryHealth);batteryPluggedContent = findViewById(R.id.batteryPlugged);batteryPresentContent = findViewById(R.id.batteryPresent);batteryStatusContent = findViewById(R.id.batteryStatus);batteryTechnologyContent = findViewById(R.id.batteryTechnology);batteryTemperatureContent = findViewById(R.id.batteryTemperature);}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(batteryLevelReceiver);}private BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {//Get battery levelbatteryLevel = getBatteryLevel(intent);batteryLevelContent.setText("" + batteryLevel);// Get battery voltagebatteryVoltage = getBatteryVoltage(intent);batteryVoltageContent.setText("" + batteryVoltage);// Get battery healthbatteryHealth = getBatteryHealth(intent);batteryHealthContent.setText("" + batteryHealth);// Get battery pluggedbatteryPlugged  = getBatteryPlugged(intent);batteryPluggedContent.setText("" + batteryPlugged);// Get battery presentbatteryPresent = getBatteryPresent(intent);batteryPresentContent.setText("" + batteryPresent);// Get battery statusbatteryStatus = getBatteryStatus(intent);batteryStatusContent.setText("" + batteryStatus);// Get battery technologybatteryTechnology = getBatteryTechnology(intent);batteryTechnologyContent.setText("" + batteryTechnology);// Get battery temperaturebatteryTemperature = getBatteryTemperature(intent);batteryTemperatureContent.setText("" + batteryTemperature);}private double getBatteryTemperature(Intent intent) {int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); // EXTRA_TEMPERATURE 包含当前电池温度的整数double batteryTempDouble = temperature / 10.0;Log.d(TAG, "getBatteryTemperature: " + batteryTemperature);return batteryTempDouble;}private String getBatteryTechnology(Intent intent) {String technologyString = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);   // EXTRA_TECHNOLOGY 描述当前电池技术的字符串Log.d(TAG, "getBatteryTechnology: " + technologyString);return technologyString;}private String getBatteryStatus(Intent intent) {String statusString;int statusInt = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);    // EXTRA_STATUS 包含当前状态常量的整数switch (statusInt){case BatteryManager.BATTERY_STATUS_CHARGING:statusString = "BATTERY_STATUS_CHARGING";break;case BatteryManager.BATTERY_STATUS_DISCHARGING:statusString = "BATTERY_STATUS_DISCHARGING";break;case BatteryManager.BATTERY_STATUS_FULL:statusString = "BATTERY_STATUS_FULL";break;case BatteryManager.BATTERY_STATUS_NOT_CHARGING:statusString = "BATTERY_STATUS_NOT_CHARGING";break;case BatteryManager.BATTERY_STATUS_UNKNOWN:statusString = "BATTERY_STATUS_UNKNOWN";break;default:statusString = "UNKNOWN";break;}Log.d(TAG, "getBatteryStatus: " + statusString);return statusString;}private String getBatteryPresent(Intent intent) {String presentString;boolean presentBool = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);  //EXTRA_PRESENT 布尔值,表示电池是否存在if (presentBool){presentString = "TRUE";}else {presentString = "FALSE";}Log.d(TAG, "getBatteryPresent: " + presentString);return presentString;}private String getBatteryPlugged(Intent intent) {String pluggedSrting;int pluggedInt = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);   //EXTRA_PLUGGED 表示设备是否插入电源的整数; 0表示使用电池,其他常数是不同类型的电源。switch (pluggedInt){case BatteryManager.BATTERY_PLUGGED_AC:     // 电源是一个交流充电器。pluggedSrting = "BATTERY_PLUGGED_AC";break;case BatteryManager.BATTERY_PLUGGED_USB:    //  电源是一个USB端口pluggedSrting = "BATTERY_PLUGGED_USB";break;case BatteryManager.BATTERY_PLUGGED_WIRELESS:   //  电源是无线的pluggedSrting = "BATTERY_PLUGGED_WIRELESS";break;default:pluggedSrting = "UNKNOWN";break;}Log.d(TAG, "getBatteryPlugged: " + pluggedSrting);return pluggedSrting;}private String getBatteryHealth(Intent intent) {String healthString;int healthInt = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);switch (healthInt){case BatteryManager.BATTERY_HEALTH_COLD:    //BATTERY_HEALTH_COLDhealthString = "BATTERY_HEALD_COLD";break;case BatteryManager.BATTERY_HEALTH_DEAD:healthString = "BATTERY_HEALTH_DEAD";break;case BatteryManager.BATTERY_HEALTH_GOOD:healthString = "BATTERY_HEALTH_GOOD";break;case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:healthString = "BATTERY_HEALTH_OVER_VOLTAGE";break;case BatteryManager.BATTERY_HEALTH_OVERHEAT:healthString = "BATTERY_HEALTH_OVERHEAT";break;case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:healthString = "BATTERY_HEALTH_UNSPECIFIED_FAILURE";break;default:healthString = "UNKNOWN";break;}Log.d(TAG, "getBatteryHealth: " + healthString);return healthString;}private double getBatteryVoltage(Intent intent) {int voltageMV = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); // EXTRA_VOLTAGE 包含当前电池电压的整数。double voltageV = voltageMV / 1000.0;Log.d(TAG, "getBatteryVoltage: " + voltageV);return voltageV;}private double getBatteryLevel(Intent intent) {int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);   //EXTRA_LEVEL 包含当前电池电量的整数字段,从0到 EXTRA_SCALEint scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  // EXTRA_SCALE 包含最大电池电量的整数。double level = -1.0;if ((rawlevel >=0) && (scale >0)){level = (double) (rawlevel * 100) / scale;}Log.d(TAG, "getBatteryLevel: " + level);return level;}};
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"><RelativeLayoutandroid:id="@+id/relative_01"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/batteryLevel_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Level: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryLevel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryLevel_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_01"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryVoltage_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Voltage: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryVoltage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryVoltage_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_02"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryHealth_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Health: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryHealth"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryHealth_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_03"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryPlugged_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Plugged: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryPlugged"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryPlugged_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_04"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryPresent_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Present: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryPresent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryPresent_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_05"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryStatus_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Status: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryStatus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryStatus_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_06"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryTechnology_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Technology: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryTechnology"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryTechnology_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout><RelativeLayoutandroid:id="@+id/relative_08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/relative_07"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/batteryTemperature_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Battery Temperature: \t"android:textColor="@color/deep_blue"/><TextViewandroid:id="@+id/batteryTemperature"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@+id/batteryTemperature_title"android:text="NA"android:textColor="@color/coral_red"android:textStyle="italic|bold" /></RelativeLayout>
</RelativeLayout>

result
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/7482.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2023年大学计算机专业实习心得14篇

2023年大学计算机专业实习心得精选篇1 20__年已然向我们挥手告别而去了。在20__年初之际&#xff0c;让我们对过去一年的工作做个总结。忙碌的一年里&#xff0c;在领导及各位同事的帮助下&#xff0c;我顺利的完成了20__年的工作。为了今后更好的工作&#xff0c;总结经验&…

Docker|kubernetes|本地镜像批量推送到Harbor私有仓库的脚本

前言&#xff1a; 可能有测试环境&#xff0c;而测试环境下有N多的镜像&#xff0c;需要批量导入到自己搭建的Harbor私有仓库内&#xff0c;一般涉及到批量的操作&#xff0c;自然还是使用脚本比较方便。 本文将介绍如何把某个服务器的本地镜像 推送到带有安全证书的私有Harb…

GAMES101笔记 Lecture07 Shading1(Illumination, Shading and Graphics Pipeline)

目录 Visibility / Occlusion(可见性 or 遮挡)Painters Algorithm(画家算法)Z-Buffer(深度缓冲算法) Shading(着色)A Simple Shading Model(Blinn-Phong Reflectance Model)一个简单的着色模型&#xff1a;Blinn-Phong反射模型Diffuse Reflection(漫反射) 参考资源 Visibility …

性能测试该怎么做,终于找到方法了

目录 开头 分类 服务器与场景设计 计算TPS 设计场景 场景运用 单交易最大压力&#xff1a; 单交易稳定性&#xff1a; 混合场景稳定性&#xff1a; 业务指标&#xff1a; 数据库 中间件 负载均衡&#xff1a; 最后&#xff1a; 开头 性能测试的工具有很多&#xf…

FreeRTOS学习笔记—基础知识

文章目录 一、什么是RTOS二、前后台系统三、实时内核&#xff08;可剥夺型内核&#xff09;四、RTOS系统五、FreeRTOS系统简介六、FreeRTOS源码下载 一、什么是RTOS RTOS全称为:Real Time OS&#xff0c;就是实时操作系统&#xff0c;核心在于实时性。实时操作系统又分为硬实时…

Servlet

1.Servlet是什么 Servlet是一种实现动态页面的技术。是一组Tomcat提供给程序员的API&#xff0c;帮助程序员简单高效的开发一个web app 回顾 动态页面 VS 静态页面 静态页面也就是内容固定的页面&#xff0c;即使 用户不同/时间不同/输入参数不同&#xff0c;页面的内容也不…

LVS负载均衡群集

目录 企业集群的应用 1、什么是集群 2、集群使用在哪个场景 3、集群分类(三种 )集群类型 负载均衡 高可用 高性能运算 4、负载均衡集群的架构 5、负载均衡集群工作模式 6、LVS虚拟服务器 LVS ipvsadm LVS和nginx比较 7、LVS负载调度算法 8、案例LVS-NAT部署实战 企…

数学建模——曲线拟合

一、曲线拟合简介 1、曲线拟合问题的提法 已知一组数据&#xff08;二维&#xff09;&#xff0c;即平面上n个点 (xi,yi)(i1,2,…,n)&#xff0c; xi互不相同。寻求一个函数yf(x)&#xff0c;使得f(x)在某种准则下与所有的数据点最为接近&#xff0c;即拟合得最好。 2、…

【雕爷学编程】Arduino动手做(149)---MAX9814咪头传感器模块

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

docker框架02docker的安装

01.这次的docker是在centos版本下的Linux系统中安装的。 02.输入命令 01.先去卸载就得版本 02.安装工具包&#xff0c;和设置镜像仓库 03.由于网络的问题&#xff0c;访问国内的阿里云镜像 修改&#xff1a; 04.更新索引和安装社区版的docker 05.启动docker 06.用命令d…

postman不能进行并发测试

1.按照网上文档的配置 2.在登录接口里睡眠5s&#xff0c;如果是并发的话&#xff0c;所有的请求都会一起睡眠5s 3.测试结果&#xff1a;请求是每隔5s串行执行的

Microsoft遭遇DDoS攻击,3000万客户数据遭窃

6月初&#xff0c;微软部分服务遭遇严重中断&#xff0c;包括Outlook电子邮件、OneDrive文件共享应用程序和云计算基础设施Azure。 一个名为”匿名苏丹”的(又名“风暴-1359”)的组织声称对此次DDoS攻击负责。 匿名苏丹组织自2023年1月以来一直活动频繁&#xff0c;声称其目标…