Android_(自动化)自动获取手机电池的剩余电量
自动获取手机电池的剩余电量
通过使用BroadcastReceiver的特性来获取手机电池的电量,注册BroadcastReceiver时设置的IntentFilter来获取系统发出的Intent.ACTION_BATTERY_CHANGED,然后以此来获取电池的电量。
运行截图:

程序结构

package com.example.asus.gary_040a; import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { private int intLevel;
private int intScale;
private Button mButton01;
private TextView tv;
//创建BroadcastReceiver
private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//如果捕捉到的Action是ACTION_BATTERY_CHANGED则运行onBatteryInforECEIVER()
if(intent.ACTION_BATTERY_CHANGED.equals(action))
{
//获得当前电量
intLevel = intent.getIntExtra("level",0);
//获得手机总电量
intScale = intent.getIntExtra("scale",100);
// 在下面会定义这个函数,显示手机当前电量
onBatteryInfoReceiver(intLevel, intScale);
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton01 = (Button) findViewById(R.id.myButton1);
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 注册一个BroadcastReceiver,作为访问电池计量之用
registerReceiver(mBatInfoReveiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
});
} // 拦截到ACTION_BATTRY_CHANGED后要执行的动作
private void onBatteryInfoReceiver(int intLevel, int intScale) {
// TODO Auto-generated method stub
int percent = intLevel*100/ intScale;
//得到的person就是百分比电量
//不乘100得到的percent为0
tv=(TextView) findViewById(R.id.myTextView02);
tv.setText("现在的电量是"+percent+"%。");
};
}
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.asus.gary_040a.MainActivity"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gary_自动获取手机电池的剩余电量!"
android:textSize="40px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <Button
android:id="@+id/myButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" /> <TextView
android:id="@+id/myTextView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50px" /> </LinearLayout>
acvivity_main
一、界面布局
一个Button按钮,一个TextView文本框
点击Button时会在TextView上显示(手机电量)提示
二、实现程序功能
1、如果捕捉的Action是ACTION_BATTERY_CHANGED则运行onBatteryInfoReceiver()显示当前手机电量
private BroadcastReceiver mBatInfoReveiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//如果捕捉到的Action是ACTION_BATTERY_CHANGED则运行onBatteryInforECEIVER()
if(intent.ACTION_BATTERY_CHANGED.equals(action))
{
//获得当前电量
intLevel = intent.getIntExtra("level",0);
//获得手机总电量
intScale = intent.getIntExtra("scale",100);
// 在下面会定义这个函数,显示手机当前电量
onBatteryInfoReceiver(intLevel, intScale);
}
}
};
2、对Button按钮添加单击后的事件响应动作,注册系统BroadcastReceiver广播事件来访问电池电量
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton01 = (Button) findViewById(R.id.myButton1);
mButton01.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 注册一个BroadcastReceiver,作为访问电池计量之用
registerReceiver(mBatInfoReveiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
});
}
3、定义方法onBatteryInfoReceiver(),通过这个方法能在TextView文本框上显示手机剩余电量
// 拦截到ACTION_BATTRY_CHANGED后要执行的动作
private void onBatteryInfoReceiver(int intLevel, int intScale) {
// TODO Auto-generated method stub
int percent = intLevel*100/ intScale;
//得到的person就是百分比电量
//不乘100得到的percent为0
tv=(TextView) findViewById(R.id.myTextView02);
tv.setText("现在的电量是"+percent+"%。");
};
Android_(自动化)自动获取手机电池的剩余电量的更多相关文章
- Android_设备隐私获取,忽略6.0权限管理
1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...
- 使用Empire自动获取域管理员
使用Empire自动获取域管理员 译:backlion 前言 自从Empire和BloodHound被应用来,对AD渗透已经可以获取到内网环境95%的信息量.作者发现自己一遍又一遍地在做同样重复的事 ...
- expect脚本自动获取root权限
Linux脚本中有很多场景是进行远程操作的,例如远程登录ssh.远程复制scp.文件传输sftp等.这些命令中都会涉及到安全密码的输入,正常使用命令时是需要人工手动输入密码并接受安全验证的.为了实现自 ...
- <自动化测试>之<自动获取手机短信验证码>
第一次写博,最近解决了做自动化测试短信验证码自动获取填入的方法减少了脚本的人工干预,并非拦截短信,所以不存在安全警报提醒,拿出来分享给大家,有感兴趣的大家可以加Q1856100 目前在职测试开发,,写 ...
- 使用 shell 脚本自动获取发版指标数据
问题背景 大一点的公司都会建立一套规章流程来避免低级错误,例如合入代码前必需经过同行评审:上线前必需提测且通过 QA 验证:全量前必需经过 1%.5%.10%.20%.50% 的灰度过程.尤其是最后一 ...
- 转载-centos网络配置(手动设置,自动获取)的2种方法
转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...
- 分享一个快速设置背景的js 自动获取背景图的长宽
我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...
- paip.enhes efis 自动获取文件的中文编码
paip.enhes efis 自动获取文件的中文编码 ##为什么需要自动获取文件的中文编码 提高开发效率,自动获取文件的中文编码 .不需要手动设置编码...轻松的.. ##cpdetector 可 ...
- 关于启明星系统移除apppath配置,让系统自动获取路径来设置cookie的解决方法
启明星系统底层使用统一接口,特别是用户,用户登录后,都会建立一个 userinfo 的cookie.请看下面2个网址: http://120.24.86.232/book http://120.24. ...
随机推荐
- 日历控件datetimepicker(IE11)
1.安装 smalot.bootstrap-datetimepicker 2.引用 bootstrap.css bootstrap-datetimepicker.min.css jquery-1.10 ...
- sp_addlinkedserver 跨服务器连接数据库查询
----创建远程链接服务器--exec sys.sp_addlinkedserver --@server='LMS',--被访问的服务器别名--@srvproduct='',--@provider=' ...
- Java list的实现类
Java list的实现类 本文是根据博文整理 Java中,List接口一共有三个实现类:ArrayList.Vector和LinkedList. 其中ArrayList和Vector都是利用数组这一 ...
- 海量数据处理 从哈希存储到Bloom Filter(1) (转载)
先解释一下什么是哈希函数.哈希函数简单来说就是一种映射,它可取值的范围(定义域)通常很大,但值域相对较小.哈希函数所作的工作就是将一个很大定义域内的值映射到一个相对较小的值域内. 传统的哈希存储 假设 ...
- python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重
from bs4 import BeautifulSoupimport sys,os,requests,pymongo,timefrom lxml import etreedef get_fenlei ...
- Redis之各版本特性
1.Redis2.6 Redis2.6在2012年正是发布,经历了17个版本,到2.6.17版本,相对于Redis2.4,主要特性如下: 1)服务端支持Lua脚本. 2)去掉虚拟内存相关功能. 3)放 ...
- css 单位
CSS 有几个不同的单位用于表示长度. 一些设置 CSS 长度的属性有 width, margin, padding, font-size, border-width, 等. 长度有一个数字和单位组成 ...
- linux下内存检测工具的使用和对比
linux背后隐藏着各种丰富的工具,学会这些工具,让这些工具更好地服务于我们的项目开发,不仅可以提高工作的效率,而且可以增强个人技术力. 参考:http://blog.chinaunix.net/ui ...
- ASE19团队项目alpha阶段model组 scrum2 记录
本次会议于11月4日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...
- SQL Server 2005 实现数据库同步备份 过程--结果---分析
数据库复制: 简单来说,数据库复制就是由两台服务器,主服务器和备份服务器,主服务器修改后,备份服务器自动修改. 复制的模式有两种:推送模式和请求模式,推送模式是主服务器修改后,自动发给备份服务器, ...