[置顶] 【Android】Android开发点击查看手机电量的小功能。学习广播的一个小技能小Demo
2016-05-30 09:37
726 查看
作者:程序员小冰,GitHub主页:https://github.com/QQ986945193
微博:http://weibo.com/mcxiaobing
首先给大家看一下效果图:
先写一个广播类:
然后再用一个主类进行动态注册广播调用即可:
项目源代码下载地址:
https://github.com/QQ986945193/PhoneBroadcastReceiverShowElectricity
微博: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
相关文章推荐
- 安卓如何调用系统自带的拨号activity
- JNIk开发过程中遇到的多线程处理问题
- 解决android系统唤醒时间偏长------看log找可疑的地方
- ubuntu android studio "Launch Standalone SDK Manager"无反应 /tools/android java not found 解决方法
- 在android studio中配置GreenDao
- Android开发中遇到的问题之XML
- gridview安卓实现单行多列横向滚动
- Android Fragment 深度解析
- Android高效加载大图、多图解决方案,有效避免程序OOM
- Android官方开发文档Training系列课程中文版:动画视图之转场框架介绍
- Android 开发中遇到的问题、坑,以及解决的想法、猜测
- Android开发新手学习总结(六)——android开发目录结构【图文版】
- Android screenrecord录屏教程
- Android 源码系列之<七>从源码的角度深入理解IntentService及HandlerThread
- Android junit单元测试
- [LeetCode] Android Unlock Patterns 安卓解锁模式
- 界面中显示NumberPicker,修改字体大小
- 读书笔记(6) Android的Drawable
- 读书笔记(7) Android动画深入分析
- geekband android #5 第十三次作业分析过程