1page.title=Manipulating Broadcast Receivers On Demand 2parent.title=Optimizing Battery Life 3parent.link=index.html 4 5trainingnavtop=true 6 7previous.title=Determining and Monitoring the Connectivity Status 8previous.link=connectivity-monitoring.html 9 10@jd:body 11 12<div id="tb-wrapper"> 13<div id="tb"> 14 15<h2>This lesson teaches you to</h2> 16<ol> 17 <li><a href="#ToggleReceivers">Toggle and Cascade State Change Receivers to Improve 18Efficiency</a></li> 19</ol> 20 21 22<h2>You should also read</h2> 23<ul> 24 <li><a href="/topic/performance/background-optimization.html">Background Optimizations</a> 25 <li><a href="/topic/performance/scheduling.html">Intelligent Job-Scheduling</a> 26 <li><a href="/training/articles/perf-anr.html">Keeping Your App Responsive</a> 27 <li><a href="/guide/components/intents-filters.html">Intents and Intent Filters</a> 28</ul> 29 30</div> 31</div> 32 33<p>The simplest way to monitor device state changes is to create a {@link 34android.content.BroadcastReceiver} for each state you're monitoring and register each of them in 35your application manifest. Then within each of these receivers you simply reschedule your recurring 36alarms based on the current device state.</p> 37 38<p>A better approach is to disable or enable the broadcast receivers at runtime. That way you can 39use the receivers you declared in the manifest as passive alarms that are triggered by system events 40only when necessary.</p> 41 42<p class="warning"> 43 <strong>Warning:</strong> Limit how many broadcast 44 receivers you set in your app. Instead of using broadcast receivers, 45 consider using other APIs for 46 performing background work. For example, in Android 5.0 (API level 21) and 47 higher, you can use the {@link android.app.job.JobScheduler} class for 48 assigning work to be completed in the background. 49 For more information about APIs you can use instead of the 50 {@link android.content.BroadcastReceiver} class for scheduling background 51 work, see 52 <a href="{@docRoot}topic/performance/background-optimization.html">Background Optimizations</a>. 53</p> 54 55<h2 id="ToggleReceivers">Toggle and Cascade State Change Receivers to Improve Efficiency </h2> 56 57<p>You can use the {@link android.content.pm.PackageManager} to toggle the enabled state on any 58component defined in the manifest, including whichever broadcast receivers you wish to enable or 59disable as shown in the snippet below:</p> 60 61<pre>ComponentName receiver = new ComponentName(context, myReceiver.class); 62 63PackageManager pm = context.getPackageManager(); 64 65pm.setComponentEnabledSetting(receiver, 66 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 67 PackageManager.DONT_KILL_APP)</pre> 68 69<p>Using this technique, if you determine that connectivity has been lost, you can disable all of 70your receivers except the connectivity-change receiver. Conversely, once you are connected you can 71stop listening for connectivity changes and simply check to see if you're online immediately before 72performing an update and rescheduling a recurring update alarm.</p> 73 74<p>You can use the same technique to delay a download that requires higher bandwidth to complete. 75Simply enable a broadcast receiver that listens for connectivity changes and initiates the 76download only after you are connected to Wi-Fi.</p> 77