1page.title=Determining and Monitoring the Connectivity Status
2parent.title=Optimizing Battery Life
3parent.link=index.html
4
5trainingnavtop=true
6
7previous.title=Determining and Monitoring the Docking State and Type
8previous.link=docking-monitoring.html
9next.title=Manipulating Broadcast Receivers On Demand
10next.link=manifest-receivers.html
11
12@jd:body
13
14<div id="tb-wrapper">
15<div id="tb">
16
17<h2>This lesson teaches you to</h2>
18<ol>
19  <li><a href="#DetermineConnection">Determine if you Have an Internet Connection</a></li>
20  <li><a href="#DetermineType">Determine the Type of your Internet Connection</a></li>
21  <li><a href="#MonitorChanges">Monitor for Changes in Connectivity</a></li>
22</ol>
23
24
25<h2>You should also read</h2>
26<ul>
27  <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent Filters</a>
28</ul>
29
30</div>
31</div>
32
33<p>Some of the most common uses for repeating alarms and background services is to schedule regular
34updates of application data from Internet resources, cache data, or execute long running downloads.
35But if you aren't connected to the Internet, or the connection is too slow to complete your
36download, why both waking the device to schedule the update at all?</p>
37
38<p>You can use the {@link android.net.ConnectivityManager} to check that you're actually
39connected to the Internet, and if so, what type of connection is in place.</p>
40
41
42<h2 id="DetermineConnection">Determine if You Have an Internet Connection</h2>
43
44<p>There's no need to schedule an update based on an Internet resource if you aren't connected to
45the Internet. The following snippet shows how to use the {@link android.net.ConnectivityManager}
46to query the active network and determine if it has Internet connectivity.</p>
47
48<pre>ConnectivityManager cm =
49        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
50
51NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
52boolean isConnected = activeNetwork != null &&
53                      activeNetwork.isConnectedOrConnecting();</pre>
54
55
56<h2 id="DetermineType">Determine the Type of your Internet Connection</h2>
57
58<p>It's also possible to determine the type of Internet connection currently available.</p>
59
60<p>Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. By
61querying the type of the active network, as shown below, you can alter your refresh rate based on
62the bandwidth available.</p>
63
64<pre>boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;</pre>
65
66<p>Mobile data costs tend to be significantly higher than Wi-Fi, so in most cases, your app's update
67rate should be lower when on mobile connections. Similarly, downloads of significant size should be
68suspended until you have a Wi-Fi connection.</p>
69
70<p>Having disabled your updates, it's important that you listen for changes in connectivity in order
71to resume them once an Internet connection has been established.</p>
72
73
74<h2 id="MonitorChanges">Monitor for Changes in Connectivity</h2>
75
76<p>The {@link android.net.ConnectivityManager} broadcasts the {@link
77android.net.ConnectivityManager#CONNECTIVITY_ACTION} ({@code
78"android.net.conn.CONNECTIVITY_CHANGE"}) action whenever the connectivity details have changed. You
79can register a broadcast receiver in your manifest to listen for these changes and resume (or
80suspend) your background updates accordingly.</p>
81
82<pre>&lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE"/></pre>
83
84<p>Changes to a device's connectivity can be very frequent&mdash;this broadcast is triggered
85every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor
86this broadcast only when you've previously suspended updates or downloads in order to resume them.
87It's generally sufficient to simply check for Internet connectivity before beginning an update and,
88should there be none, suspend further updates until connectivity is restored.</p>
89
90<p>This technique requires toggling broadcast receivers you've declared in the manifest, which is
91described in the next lesson.</p>
92