blob: 5efa2facf901911b498c25b251e31f5a0f9af978 [file] [log] [blame]
page.title=Manipulating Broadcast Receivers On Demand
parent.title=Optimizing Battery Life
parent.link=index.html
trainingnavtop=true
previous.title=Determining and Monitoring the Connectivity Status
previous.link=connectivity-monitoring.html
@jd:body
<div id="tb-wrapper">
<div id="tb">
<h2>This lesson teaches you to</h2>
<ol>
<li><a href="#ToggleReceivers">Toggle and Cascade State Change Receivers to Improve
Efficiency</a></li>
</ol>
<h2>You should also read</h2>
<ul>
<li><a href="/topic/performance/background-optimization.html">Background Optimizations</a>
<li><a href="/topic/performance/scheduling.html">Intelligent Job-Scheduling</a>
<li><a href="/training/articles/perf-anr.html">Keeping Your App Responsive</a>
<li><a href="/guide/components/intents-filters.html">Intents and Intent Filters</a>
</ul>
</div>
</div>
<p>The simplest way to monitor device state changes is to create a {@link
android.content.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.</p>
<p>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.</p>
<p class="warning">
<strong>Warning:</strong> Limit how many broadcast
receivers you set in your app. Instead of using broadcast receivers,
consider using other APIs for
performing background work. For example, in Android 5.0 (API level 21) and
higher, you can use the {@link android.app.job.JobScheduler} class for
assigning work to be completed in the background.
For more information about APIs you can use instead of the
{@link android.content.BroadcastReceiver} class for scheduling background
work, see
<a href="{@docRoot}topic/performance/background-optimization.html">Background Optimizations</a>.
</p>
<h2 id="ToggleReceivers">Toggle and Cascade State Change Receivers to Improve Efficiency </h2>
<p>You can use the {@link android.content.pm.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:</p>
<pre>ComponentName receiver = new ComponentName(context, myReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP)</pre>
<p>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.</p>
<p>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.</p>