Android电池信息获取
Android 可以通过BroadcastReceiver来获取电池信息改变的广播(ACTION_BATTERY_CHANGED),从而获取到相关的电池信息。
电池信息,及其对应的相关常数(参考网址:http://blog.sina.com.cn/s/blog_5d2e69770102vh59.html)
电池信息 | 类型 | 备注 |
status | int | 取得电池的状态,返回的状态类型由 android.os.BatteryManager 类定义的常量所决定,包括: |
电池充电状态( BATTERY_STATUS_CHARGING ) | ||
电池放电状态( BATTERY_STATUS_DISCHARGING ) | ||
电池满电状态( BATTERY_STATUS_FULL ) | ||
电池不充电状态( BATTERY_STATUS_NOT_CHARGING ) | ||
电池未知状态( BATTERY_STATUS_UNKNOWN ) | ||
health | int | 取得电池的健康状态,返回的状态类型由 android.os.BatteryManager 类定义的常量所决定,包括: |
电池损坏( BATTERY_HEALTH_DEAD ) | ||
电池健康( BATTERY_HEALTH_GOOD ) | ||
电池过热( BATTERY_HEALTH_OVERHEAT ) | ||
电池电压过大( BATTERY_HEALTH_OVER_VOLTAGE ) | ||
未知状态( BATTERY_HEALTH_UNKOWN ) | ||
未明示故障( BATTERY_HEALTH_UNSPECIFIED_FAILURE ) | ||
present | boolean | 判断当前是否存在电池 |
level | int | 取得电池的剩余容量 |
scale | int | 取得电池的总容量,通常为 100 |
Icon-small | int | 取得电池对应的图标 ID |
plugged | int | 连接的电源插座类型,返回的状态由 android.os.BatteryManager 类定义的常量所决定,包括: |
USB 电源( BATTERY_PLUGGED_USB ) | ||
交流电电源( BATTERY_PLUGGED_AC ) | ||
voltage | int | 取得电池的电压 |
temperature | int | 取得电池的温度,单位是摄氏度 |
technology | String | 取得电池的类型 |
获取电池信息的步骤:
(1) 创建BroadcastReceiver,用于结束电池信息变化的广播
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
//TO GET BatteryInfo
}
(2) 重写onResume()方法,获取IntentFilter对象,并注册BroadcastReceiver
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
super.onResume();
}
(3)程序在后台的时候取消注册BroadcastReceiver
protected void onPause() {
unregisterReceiver(mBroadcastReceiver);
super.onPause();
}
实例:
public class GetBatteryInfo extends Activity {
private static final int REFRESH_TIME = 1;
private static boolean runtime=false;
private TextView mStatus_BatteryInfo;
private TextView mPlugged_BatteryInfo;
private TextView mLevel_BatteryInfo;
private TextView mScale_BatteryInfo;
private TextView mVoltage_BatteryInfo;
private TextView mTemperature_BatteryInfo;
private TextView mTechnology_BatteryInfo;
private TextView mHealth_BatteryInfo;
private TextView mRuntime_BatteryInfo;
private TimeThread timeThread; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();
} private void InitView() {
mStatus_BatteryInfo=(TextView)findViewById(R.id.status_BatteryInfo);
mPlugged_BatteryInfo=(TextView)findViewById(R.id.plugged_BatteryInfo);
mLevel_BatteryInfo=(TextView)findViewById(R.id.level_BatteryInfo);
mScale_BatteryInfo=(TextView)findViewById(R.id.scale_BatteryInfo);
mVoltage_BatteryInfo=(TextView)findViewById(R.id.voltage_BatteryInfo);
mTemperature_BatteryInfo=(TextView)findViewById(R.id.temperature_BatteryInfo);
mTechnology_BatteryInfo=(TextView)findViewById(R.id.technology_BatteryInfo);
mHealth_BatteryInfo=(TextView)findViewById(R.id.health_BatteryInfo);
mRuntime_BatteryInfo=(TextView)findViewById(R.id.runtime_BatteryInfo);
} @Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
startTimeThread(timeThread);
super.onResume();
} @Override
protected void onPause() {
stopTimeThread(timeThread);
unregisterReceiver(mBroadcastReceiver);
super.onPause();
} /**
* @param milliseconds 将ms转化为hh:mm:ss 格式时间
* @return
*/
private String msToTime(int milliseconds ){
int allSeconds=(milliseconds/1000);
int hours=allSeconds/3600;
String hours_string;
int feelSeconds=allSeconds%3600;
int mimute=feelSeconds/60;
String mimute_string;
String second_string;
int second=feelSeconds%60;
if(hours<10){
hours_string="0"+String.valueOf(hours);
}else{
hours_string=String.valueOf(hours);
}
if(mimute<10){
mimute_string="0"+String.valueOf(mimute);
}else{
mimute_string=String.valueOf(mimute);
}
if(second<10){
second_string="0"+String.valueOf(second);
}else{
second_string=String.valueOf(second);
}
return hours_string+":"+mimute_string+":"+second_string;
} private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
int status = intent.getIntExtra("status", 0);
int health = intent.getIntExtra("health", 0);
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 0);
int plugged = intent.getIntExtra("plugged", 0);
int voltage = intent.getIntExtra("voltage", 0);
int temperature = intent.getIntExtra("temperature", 0);
String technology = intent.getStringExtra("technology");
mLevel_BatteryInfo.setText(level+"");
mScale_BatteryInfo.setText(scale+"");
mVoltage_BatteryInfo.setText(voltage+"mV");
mTemperature_BatteryInfo.setText(((float)temperature/10)+"℃");
mTechnology_BatteryInfo.setText(technology+"");
mRuntime_BatteryInfo.setText(msToTime((int) SystemClock.elapsedRealtime()));
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_UNKNOWN));
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_CHARGING));
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_DISCHARGING));
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_NOT_CHARGING));
break;
case BatteryManager.BATTERY_STATUS_FULL:
mStatus_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_status_FULL));
break;
}
switch (health) {
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_UNKOWN));
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_GOOD));
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_OVERHEAT));
break;
case BatteryManager.BATTERY_HEALTH_DEAD:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_DEAD));
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_VOLTAGE));
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
mHealth_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_health_UNSPECIFIED_FAILURE));
break;
} switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
mPlugged_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_plugged_AC));
break;
case BatteryManager.BATTERY_PLUGGED_USB:
mPlugged_BatteryInfo.setText(getResources().getString(R.string.BatteryInfo_plugged_USB));
break;
default:
mPlugged_BatteryInfo.setText("");
}
}
}
}; Handler TimeHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case REFRESH_TIME:
mRuntime_BatteryInfo.setText(msToTime((int) SystemClock.elapsedRealtime()));//获取系统启动时间
break;
default:
break;
} super.handleMessage(msg);
} }; /**
* 打开线程timeThread
* @param timeThread
* @param runtime 运行标志位
*/
private void startTimeThread(Thread timeThread){
if(timeThread==null){
runtime=true;
timeThread=new TimeThread();
timeThread.start();
}
} /**
* 停止线程
* @param timeThread
*/
private void stopTimeThread(Thread timeThread){
if(timeThread!=null){
Thread.currentThread().interrupt();
timeThread.interrupt();
runtime=false;
} } class TimeThread extends Thread{
@Override
public void run() {
while(runtime){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg=new Message();
msg.what=REFRESH_TIME;
TimeHandler.sendMessage(msg);
}
} } }
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#5CD8AF"
android:gravity="center_vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="@string/SystemSetBatteryInfo_BatteryInfo_textvie"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_status_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/status_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_plugged_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/plugged_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_level_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/level_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_scale_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/scale_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_voltage_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/voltage_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_temperature_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/temperature_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_technology_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/technology_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_health_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/health_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/SystemSetBatteryInfo_runtime_textview"
android:textColor="@color/main_view_color"
android:textSize="18sp" /> <TextView
android:id="@+id/runtime_BatteryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/main_view_color"
android:textSize="18sp" />
</LinearLayout> </LinearLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">GetBatteryInfo</string>
<string name="SystemSetBatteryInfo_BatteryInfo_textvie">电池信息</string>
<string name="SystemSetBatteryInfo_status_textview">电池状态:</string>
<string name="SystemSetBatteryInfo_plugged_textview">充电方式:</string>
<string name="SystemSetBatteryInfo_level_textview">电池电量:</string>
<string name="SystemSetBatteryInfo_scale_textview">电池容量:</string>
<string name="SystemSetBatteryInfo_voltage_textview">电池电压:</string>
<string name="SystemSetBatteryInfo_temperature_textview">电池温度:</string>
<string name="SystemSetBatteryInfo_technology_textview">电池类型:</string>
<string name="SystemSetBatteryInfo_health_textview">健康状态:</string>
<string name="SystemSetBatteryInfo_runtime_textview">使用时间:</string>
<string name="BatteryInfo_status_CHARGING">正在充电</string>
<string name="BatteryInfo_status_DISCHARGING">正在耗电</string>
<string name="BatteryInfo_status_FULL">电量充满</string>
<string name="BatteryInfo_status_NOT_CHARGING ">未充电</string>
<string name="BatteryInfo_status_UNKNOWN">状态未知</string>
<string name="BatteryInfo_plugged_USB">USB电源</string>
<string name="BatteryInfo_plugged_AC">交流电电源</string>
<string name="BatteryInfo_health_DEAD">电池损坏</string>
<string name="BatteryInfo_health_GOOD">电池正常</string>
<string name="BatteryInfo_health_OVERHEAT">电池过热</string>
<string name="BatteryInfo_health_VOLTAGE ">电池电压过大</string>
<string name="BatteryInfo_health_UNKOWN">未知状态</string>
<string name="BatteryInfo_health_UNSPECIFIED_FAILURE">未明示故障</string> </resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_view_color">#1f1f1f</color>
<color name="main_text_color">#6f6f6f</color>
</resources>
Android电池信息获取的更多相关文章
- android电池信息简介
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- android WIFI信息获取
在androi中WIFI信息的获取能够通过系统提供的WIFI Service获取 [java] WifiManager wifi_service = (WifiManager)getSystemSe ...
- Android屏幕信息获取
Android中有时需要获取屏幕的size信息以便对控件位置进行动态控制,最近做了一些研究,现在将获取屏幕大小信息的方法总结如下,可能存在一些地方理解的不全面. 1.getMetrics Displa ...
- android sdcard信息获取
手机存储都有两种,一种是 手机自带的存储,称为internal storage,另外一种用户额外插入的存储,称为removable storage (也就是外置sdcard的部分). removabl ...
- android 手机信息获取
1. adb已安装 2. adb shell getprop 此时已列出所有相关信息
- Android 充电信息的获取【转】
本文转载自:https://blog.csdn.net/wateryi/article/details/50834821 在android系统中,电池信息是由BatteryService.java统一 ...
- Android 使用adb查看和修改电池信息
1.获取电池信息 $ adb shell dumpsys battery $ adb shell dumpsys battery Current Battery Service state: AC p ...
- 【风马一族_Android】Android 从命令行界面获取手机信息
Android 从命令行界面获取手机信息 1: cmd 打开命令行界面 2:adb devices 获取与电脑相连的设备,例如:模拟器.真机(手机) (右击“标记”,选择设备名称,点击“Ctrl+ ...
- Android设备网络、屏幕尺寸、SD卡、本地IP、存储空间等信息获取工具类
Android设备网络.屏幕尺寸.SD卡.本地IP.存储空间.服务.进程.应用包名等信息获取的整合工具类. package com.qiyu.ddb.util; import android.anno ...
随机推荐
- Ubuntu16.04安装QQ机器人
Ubuntu安装QQ机器人 看了看现在QQ机器人似乎只有酷Q机器人有Docker可以在linux上运行了 那就k开始装酷Q机器人,资源占用也不是很大,大概占用180M内存吧 安装酷Q HTTP 首先安 ...
- Chromium EC框架探索 1.1 开发环境搭建
1.1 开发环境搭建 本节参考官方文档getting started building ec images quickly编写本节介绍搭建ec开发环境的两种方式,后一种对于绝大多数人而言是不必要的. ...
- vue的使用经验
导读 这一次的口号是,带你重新认识vue,拯救1,2个前端开发者. 从2018年从事前端职业以来,呆过大小公司,干这一行都有2年多.代码写的多了,就越来越体会新手程序员都是挖坑的.在工作过程中,用一种 ...
- JUC 中提供的限流利器-Semaphore(信号量)
在 JUC 包下,有一个 Semaphore 类,翻译成信号量,Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.Semaphore 跟锁 ...
- 老板说,Vim宏都不会用,你的工作效率太低啦~
工作中,对于文本文件的编辑我们经常有这样的需求: 多次重复输入一段相同文本: 生成一段序列化的文本: 每行文本插入一句相同的文本. 除此之外,还有很多需要重复操作的动作.对于这些需求,如果我们人工去操 ...
- 使用PyTorch进行迁移学习
概述 迁移学习可以改变你建立机器学习和深度学习模型的方式 了解如何使用PyTorch进行迁移学习,以及如何将其与使用预训练的模型联系起来 我们将使用真实世界的数据集,并比较使用卷积神经网络(CNNs) ...
- HDU - 1160 最长上升子序列以及记录路径
题意:第一列,给出老鼠的重量,第二列,给出老鼠的速度,要证明老鼠的重量越大,速度越小,给出最多老鼠的数量,并说明第几只. 思路:先将老鼠按照重量从大到小排序,然后速度是从小到大,求最长上升子序列,学习 ...
- Redis数据结构——quicklist
之前的文章我们曾总结到了Redis数据结构--链表和Redis数据结构--压缩列表这两种数据结构,他们是Redis List(列表)对象的底层实现方式.但是考虑到链表的附加空间相对太高,prev 和 ...
- 前端验证,jquery.validate插件
jQuery Validate 简介: jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用 ...
- unix中数据缓冲区高速缓冲的设计
目录 1. 概述 2. 缓冲区的设计 2.1 缓冲区头部 2.2 缓冲区的结构 2.3 缓冲区的检索算法 2.3. 申请一个缓冲区算法 getblk 2.3.2 释放一个缓冲区算法 brelse 2. ...