Manipulating Broadcast Receivers On Demand

  The simplest way to monitor device state changes is to create a BroadcastReceiver for each state you're monitoring and register each of them in your application manifest. Then within each of these receivers you simply reschedule your recurring alarms based on the current device state.

  A side-effect of this approach is that your app will wake the device each time any of these receivers is triggered—potentially much more frequently than required.

  A better approach is to disable or enable the broadcast receivers at runtime. That way you can use the receivers you declared in the manifest as passive alarms that are triggered by system events only when necessary.

Toggle and Cascade State Change Receivers to Improve Efficiency

  You can use the PackageManager to toggle the enabled state on any component defined in the manifest, including whichever broadcast receivers you wish to enable or disable as shown in the snippet below:

 ComponentName receiver = new ComponentName(context, myReceiver.class);

 PackageManager pm = context.getPackageManager();

 pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP)

  Using this technique, if you determine that connectivity has been lost, you can disable all of your receivers except the connectivity-change receiver. Conversely, once you are connected you can stop listening for connectivity changes and simply check to see if you're online immediately before performing an update and rescheduling a recurring update alarm.

  You can use the same technique to delay a download that requires higher bandwidth to complete. Simply enable a broadcast receiver that listens for connectivity changes and initiates the download only after you are connected to Wi-Fi.

Android 性能优化(15)网络优化( 11)Manipulating Broadcast Receivers On Demand的更多相关文章

  1. 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化

    第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...

  2. Google 发布的15个 Android 性能优化典范

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

  3. Android 性能优化(7)网络优化( 3) Optimizing User-Initiated Network Use

    Optimizing User-Initiated Network Use This lesson teaches you to Pre-fetch Network Data Check for Co ...

  4. Android性能优化典范(二)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  5. android app性能优化大汇总(google官方Android性能优化典范 - 第2季)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  6. Android性能优化典范 - 第2季

    Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...

  7. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  8. android 性能优化

    本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...

  9. Android性能优化典范第一季

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

随机推荐

  1. 【ZJOI2017 Round1后记】

    2017.4.1: NOIP+Round1综合成绩出来,标准分离续命线差了80分,果然还是联赛坑挖太大了…… 不管怎么说能续命的话还是要试一下的…… 发毒誓:Round2前不打手游,不看NGA,不看星 ...

  2. 【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

    MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link ...

  3. Free Goodies UVA - 12260

    Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...

  4. Redis集群方案之使用豌豆荚Codis搭建(待实践)

    Codis的模式类似Twemproxy,不过这东西引入了ZooKeeper做为Redis的注册与发现来实现高可用. 部署时需要额外增加应用的部署,请根据业务需求来衡量. 部署图类似如下: 当然,上面的 ...

  5. 【Windows系统】-- 远程桌面时,WIN键被锁定

    问题重现: 在对远程机器进行操作的时候,按键时会自动变成WIN组合键,比如:你按D的效果为[WIN+D]组合键的效果 就是切换到桌面,按E就是[WIN+E]组合键的效果,就是打开资源管理器. 解决方案 ...

  6. DOM 事件类

    DOM事件的级别: DOM级别一共可以分4个级别:DOM0级.DOM1级. DOM2级 .DOM3级.而DOM事件级别分为3个级别:DOM0级事件处理.DOM2级事件处理.DOM3级事件处理 1. D ...

  7. [NOI2015Day1]解题报告

    今天一起做NOI的题. 我仅仅想说SunshinAK了好神啊. T3数据好坑啊,打表竟然被编译环境卡掉了... T1:程序自己主动分析 (http://www.lydsy.com/JudgeOnlin ...

  8. 另类创业招聘(REV#2)

    项目一 项目名:苏格拉底网 项目性质:人才測评为主.辅助以简易人才招聘功能的小众功能站点.项目使用了自主研发的人才測评算法以及人格分类模型(与MBTI非常相似). 项目相关网址:sugeladi.ne ...

  9. 【bzoj3609】[Heoi2014]人人尽说江南好

    可以算出合并多少次. #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...

  10. web container和spring container之间的关系

    1 tomcat是一个web container,它提供java web应用的运行环境,因为它实现了好多java ee规范,比如java Servlet.JSP等,也就是说,没有它,java web应 ...