Manipulating Broadcast Receivers On Demand

Source: Internet
Author: User

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 EfficiencyUse 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 extends ER = new ComponentName (context, myReceiver. class); PackageManager pm = context. getPackageManager (); pm. setComponentEnabledSetting (Consumer er, 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 beyond t the connectivity-change cycler. conversely, once you are connected you can stop listening for connectivity changes and simply check to see if you're online immediately before refreshing 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 er that listens for connectivity changes and initiates the download only after you are connected to Wi-Fi.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.