用BroadcastReceiver监听电量的变化,可以实现BroadcastReceiver接收电量变化的广播,然后获取电量百分比信息。

  BatteryChangedReceiver.java

public class BatteryChangedReceiver extends BroadcastReceiver{
private static final String TAG="BatteryChangedReceiver";
@Override
public void onReceive(Context context,Intent intent){
int currLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);//当前电量
int total=intent.getIntExtra(BatteryManager.EXTRA_SCALE, 1);//总电量
int percent=currLevel*100/total;
Log.i(TAG,"battery:"+percent+"%");
}
}

  在清单文件中注册广播接收者以及要拦截的Action:

<receiver android:name=".BatteryChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
</intent-filter>
</receiver>

  有些时候,我们需要立即获取电量,而不是等电量变化的广播,可以通过下面的方式:

Intent batteryIntent=getApplicationContext().registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int currLevel=batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int total=batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE,1);
int percent=currLevel*100/total;
Log.i("battery","battery:"+percent+"%");

BroadcastReceiver监听电量变化的更多相关文章

  1. Android实现后台长期监听时间变化

    1.首先我们的目的是长期监听时间变化,事实上应用程序退出. 通过了解我们知道注冊ACTION_TIME_TICK广播接收器能够监听系统事件改变,可是 查看SDK发现ACTION_TIME_TICK广播 ...

  2. Android 5.0 以上监听网络变化

    大家好,大概有一个多月没有更新博客了,我是干什么去了呢?很明显,程序员当然要加班……这一次跟大家分享一下新项目的一些心得. 监听网络变化在开发中是经常用到的,例如我们断网有一些友好的提示,或者根据不同 ...

  3. Angular.js中使用$watch监听模型变化

    $watch简单使用 $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEqua ...

  4. $scope.$watch()——监听数据变化

    $scope.$watch(watchFn, watchAction, [deepWatch]):监听数据变化,三个参数 --watchFn:监听的对象,一个带有Angular 表达式或者函数的字符串 ...

  5. 如何使用NodeJs来监听文件变化

    1.前言 在我们调试修改代码的时候,每修改一次代码,哪怕只是很小的修改,我们都需要手动重新build文件,然后再运行代码,看修改的效果,这样的效率特别低,对于开发者来说简直不能忍. 2.构建自动编译工 ...

  6. android动态注册监听网络变化异常

    在使用广播接收器监听网络变化的时候,在AndroidManifest.xml中加入<user-permission android:name="android.permission.A ...

  7. Android 监听网络变化

    Android 监听网络变化

  8. --@angularJS--$scope.watch监听模型变化

    $watch简单使用 $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEqua ...

  9. 用BroadcastReceiver监听手机网络状态变化

    android--解决方案--用BroadcastReceiver监听手机网络状态变化 标签: android网络状态监听方案 2015-01-20 15:23 1294人阅读 评论(3) 收藏 举报 ...

随机推荐

  1. 《深入Linux内核》 UNIX的一些故事

    Unix文件的类型1.普通文件2.目录3.符号链接4.面向块的设备文件5.面向字符的设备文件6.管道和命名管道7.套接字 点评:不明觉厉 打开文件进程只能访问“打开的”文件.为了打开一个文件,进程调用 ...

  2. ural-1099-Work Scheduling(裸带花树)

    题意: 有N个人,有限对的人可以在一起工作,问最多能有多少对. 分析: 任意图的最大匹配 // File MAXName: 1099.cpp // Author: Zlbing // Created ...

  3. HDOJ(HDU) 2161 Primes(素数打表)

    Problem Description Write a program to read in a list of integers and determine whether or not each ...

  4. [Locked] Meeting Room I && II

    Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...

  5. hdoj3351-stack

    Problem Description I'm out of stories. For years I've been writing stories, some rather silly, just ...

  6. eclipse连接远程hadoop集群开发时0700问题解决方案

    eclipse连接远程hadoop集群开发时报错 错误信息: Exception in thread "main" java.io.IOException:Failed to se ...

  7. 用于A*的 二叉堆 AS3实现

    package com.copper.isometric.pathing {     import flash.sampler.startSampling;           /**      * ...

  8. android获取apk签名信息

    因为工作需要,需要获取应用的apk传递给新浪微博或者微信去申请授权 所以需要程序内获取签名上传服务器 做法如下:   public static String getAPPSecretString(A ...

  9. ios中键值编码kvc和键值监听kvo的特性及详解

    总结: kvc键值编码  1.就是在oc中可以对属性进行动态读写(以往都是自己赋值属性)           2. 如果方法属性的关键字和需要数据中的关键字相同的话                  ...

  10. Windows下Postgre SQL数据库通过Slony-I 实现数据库双机同步备份

    一. 我们要实现的环境是windows xp.windows2003上安装Postgre SQL数据库,实现目的是两台数据库服务器进行数据库同步,即数据库同步更新.删除.插入等对数据库的操作. 二. ...