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
Use 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.
分享到:
相关推荐
SAS® Programming II: Manipulating Data with the DATA Step
Manipulating Files - Delete
关于PDF文件制作及操作的经典书籍,适用于java C#等多种语言,可以添加图片,水印,各种字体,报表,教你如何一步一步的成为PDF编程高手,真正的step by step,深入浅出,五步即可制作自己的PDF文件。
In addition, the author gives helpful hints on when to use pointers and not subscripts when manipulating arrays in C. The performance issues involved in this are extremely important in scientific ...
SAS Programming II Manipulating Data with the DATA Step.rar
Manipulating individual dichlorotin phthalocyanine molecules on Cu(100) surface at roomtemperature by scanning tunneling microscopy
C library for encoding, decoding and manipulating JSON data
Series of MATLAB scripts and functions for manipulating ADCIRC meshes, interpolation of bathymetry, and calculation of specific quantities.zip
Creating Intent Filters and Broadcast Receivers Chapter 6: Using Internet Resources Downloading and Parsing Internet Resources Using the Download Manager Using Internet Services Connecting to Google ...
Manipulating expectancy in the affective domain and its effects on achievement, intelligence and personality MANIPULATING EXPECTANCY I N T H E AFFECTIVE DOMAIN AND ITS EFFECTS ON ACHIEVEMENT, ...
Manipulating_SQL_Server_Using_SQL_Injection.7z 简单实用的教程课件 学习的好帮手
Manipulating radiation is important for a variety of optoelectronic applications, such as on-chip lasers, energy-efficient grating couplers, and antennas for light detection and ranging. Although ...
练习中的代码:操纵哈希目标在本课程中,我们将仔细研究多维或嵌套的哈希,迭代和更高级别的哈希方法。在继续进行下一个实验之前,我们将共同应对一些挑战。有关构建嵌套哈希的更多信息假设我们具有以下嵌套哈希: ...
SMASheD__Sniffing_and_Manipulating_Android_Sensor_Data.pdf
Based on the concept of sub-wavelength imaging through compensated bilayer of anisotropic metamaterials (AMMs), which is an expansion of the perfect lens configuration, we propose two dimensional ...
Functions-for-Manipulating-Data-in-PostgreSQL-Datacamp
jqGrid is an Ajax-enabled JavaScript control that provides solutions for representing and manipulating tabular data on the web. Since the grid is a client-side solution loading data dynamically ...
「安全管理」SMASheD__Sniffing_and_Manipulating_Android_Sensor_Data - 无线安全 Web安全 勒索病毒 日志审计 安全知识 网站安全
《深入理解计算机系统》实验一(操作bit) 实验材料