自动获取手机电池的剩余电量

通过使用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_(自动化)自动获取手机电池的剩余电量的更多相关文章

  1. Android_设备隐私获取,忽略6.0权限管理

    1.前言 (1).由于MIUI等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私.在Android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态: ...

  2. 使用Empire自动获取域管理员

    使用Empire自动获取域管理员  译:backlion 前言 自从Empire和BloodHound被应用来,对AD渗透已经可以获取到内网环境95%的信息量.作者发现自己一遍又一遍地在做同样重复的事 ...

  3. expect脚本自动获取root权限

    Linux脚本中有很多场景是进行远程操作的,例如远程登录ssh.远程复制scp.文件传输sftp等.这些命令中都会涉及到安全密码的输入,正常使用命令时是需要人工手动输入密码并接受安全验证的.为了实现自 ...

  4. <自动化测试>之<自动获取手机短信验证码>

    第一次写博,最近解决了做自动化测试短信验证码自动获取填入的方法减少了脚本的人工干预,并非拦截短信,所以不存在安全警报提醒,拿出来分享给大家,有感兴趣的大家可以加Q1856100 目前在职测试开发,,写 ...

  5. 使用 shell 脚本自动获取发版指标数据

    问题背景 大一点的公司都会建立一套规章流程来避免低级错误,例如合入代码前必需经过同行评审:上线前必需提测且通过 QA 验证:全量前必需经过 1%.5%.10%.20%.50% 的灰度过程.尤其是最后一 ...

  6. 转载-centos网络配置(手动设置,自动获取)的2种方法

    转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...

  7. 分享一个快速设置背景的js 自动获取背景图的长宽

    我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...

  8. paip.enhes efis 自动获取文件的中文编码

    paip.enhes efis 自动获取文件的中文编码 ##为什么需要自动获取文件的中文编码 提高开发效率,自动获取文件的中文编码  .不需要手动设置编码...轻松的.. ##cpdetector 可 ...

  9. 关于启明星系统移除apppath配置,让系统自动获取路径来设置cookie的解决方法

    启明星系统底层使用统一接口,特别是用户,用户登录后,都会建立一个 userinfo 的cookie.请看下面2个网址: http://120.24.86.232/book http://120.24. ...

随机推荐

  1. HttpWorkerRequest应用简介

    1. Using HttpWorkerRequest for getting headers1.使用HttpWorkerRequest获取headers信息 First, the HttpWorker ...

  2. BZOJ5017题解SNOI2017炸弹--玄学递推

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=5017 分析 老师讲课谈到了这道题,课上想出了个连边建图然后乱搞的操作,被老师钦定的递推方 ...

  3. Java 实现 海康摄像头抓拍图像 Windows、Linux

    先抱怨一下,打死都想不到,海康的摄像头SDK居然是一个Java类,还有必须的两个jar包(jna.jar,examples.jar).鬼能想得到会这么命名. 下面开始吧. Windows 把从官网下载 ...

  4. css高度居中

    1.已知元素高度 // 子盒子 #div1{ width:200px; height:200px; position: absolute; //父元素需要相对定位 top: 50%; left: 50 ...

  5. 【0】Zookeeper Q&A

    1.Observer角色如何配置? Zookeeper集群中的中的Leader和Follower角色是由服务器启动时期的Leader选举产生的,Observer不参与选举,此角色的节点需要在配置文件z ...

  6. Odoo的菜单项

    用户界面的入口是菜单项,菜单项形成一个层级结构,最顶级项为应用,其下一级为每个应用的主菜单.还可以添加更深的子菜单.可操作菜单与窗口操作关联,它告诉客户端在点击了菜单项后应执行什么操作. 菜单项存储在 ...

  7. Python爬虫解析htm时lxml的HtmlElement对象获取和设置inner html方法

    Python的lxml是一个相当强悍的解析html.XML的模块,最新版本支持的python版本从2.6到3.6,是写爬虫的必备利器.它基于C语言库libxml2 和 libxslt,进行了Pytho ...

  8. IPC之mq_sysctl.c源码解读

    // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 IBM Corporation * * Author: Cedric ...

  9. Linux:INIT runlevel service netstat ps top pgrep kill killall jobs pkill crontab

    INIT进程 Linux内核加载执行/sbin/init程序 -Linux的第一个进程,进程ID为1 -主配置文件:/etc/ininttab init 0 关机 init 1 单用户模式 init ...

  10. Winform的高DPI问题

    现在的屏幕大部分都是高分屏,在这样的屏幕下开发winfrom软件就需要注意高DPI问题了 1.Form和UserControl的AutoScaleMode设置为Dpi 2.为项目添加应用程序清单文件( ...