作者:程序员小冰,GitHub主页:https://github.com/QQ986945193

微博:http://weibo.com/mcxiaobing

首先给大家看一下效果图:



先写一个广播类:

package david.qq986945193.com.davidphonebroadcastreceivershowelectricity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; /**
* @author :程序员小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @GitHub:https://github.com/QQ986945193
* @CSDN博客: http://blog.csdn.net/qq_21376985
* @交流Qq :986945193
*/
public class BatteryInfoBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) { int level = intent.getIntExtra("level", 0);
//默认总电量 数值范围
int scale = intent.getIntExtra("scale", 100); Dialog dialog = new AlertDialog.Builder(context).setTitle(
"电池电量"
).setMessage("电池电量为:" + String.valueOf(level * 100 / scale) + "%").setNegativeButton(
"关闭", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { }
}
).create();
dialog.show();
}
}
}

然后再用一个主类进行动态注册广播调用即可:

 /**
* 动态注册广播
*/
BatteryInfoBroadcastReceiver receiver = new BatteryInfoBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);

项目源代码下载地址:

https://github.com/QQ986945193/PhoneBroadcastReceiverShowElectricity

最新文章

  1. Redis_DataType
  2. jQuery基础_2
  3. SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法
  4. js之获取窗口大小和位置信息
  5. 将excel2003文档文件转换为excel2007格式
  6. 【原文】前端程序员必须知道的高性能Javascript知识
  7. CodeForces 7C Line
  8. DOM 对象之 document.all
  9. 在eclipse上提交任务到集群执行
  10. JAVA类型修饰符(public,protected,private,friendly)
  11. Android studio一些常见技巧(不断更新)
  12. Elasticsearch 通关教程(七): Elasticsearch 的性能优化
  13. python中的mysql数据库like模糊查询
  14. oracle出现sga导致ORA-00821 ORA-00093报错解决办法
  15. C# WPF 父控件通过使用可视化树找到子控件
  16. centos7 搭建openvpn服务器
  17. Node.js基础学习一之Get请求
  18. [Android Pro] AndroidX了解一下
  19. ELK之elasticsearch集群搭建
  20. 六:python 对象类型详解二:字符串(下)

热门文章

  1. SSH全注解-annotation详细配置
  2. Java实现短信验证码
  3. canvas小画板——(2)荧光笔效果
  4. FPN和他的子孙们
  5. Setup Factory 9 打包安装程序过程中提示安装.net4.5、并启用md5加密算法
  6. 2020-04-08:谈一下IOC底层原理
  7. IDEA 非常重要的一些设置项 → 一连串的问题差点让我重新用回 Eclipse !
  8. linux驱动之内核多线程(一)
  9. JavaScript学习系列博客_12_JavaScript中的break、continue关键字
  10. golang 判断IPv4 or IPv6 address