1 page.title=Determining and Monitoring the Connectivity Status 2 parent.title=Optimizing Battery Life 3 parent.link=index.html 4 5 trainingnavtop=true 6 7 previous.title=Determining and Monitoring the Docking State and Type 8 previous.link=docking-monitoring.html 9 next.title=Manipulating Broadcast Receivers On Demand 10 next.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 34 updates of application data from Internet resources, cache data, or execute long running downloads. 35 But if you aren't connected to the Internet, or the connection is too slow to complete your 36 download, 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 39 connected 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 45 the Internet. The following snippet shows how to use the {@link android.net.ConnectivityManager} 46 to 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 51 NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 52 boolean 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 61 querying the type of the active network, as shown below, you can alter your refresh rate based on 62 the 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 67 rate should be lower when on mobile connections. Similarly, downloads of significant size should be 68 suspended 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 71 to 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 77 android.net.ConnectivityManager#CONNECTIVITY_ACTION} ({@code 78 "android.net.conn.CONNECTIVITY_CHANGE"}) action whenever the connectivity details have changed. You 79 can register a broadcast receiver in your manifest to listen for these changes and resume (or 80 suspend) your background updates accordingly.</p> 81 82 <pre><action android:name="android.net.conn.CONNECTIVITY_CHANGE"/></pre> 83 84 <p>Changes to a device's connectivity can be very frequent—this broadcast is triggered 85 every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor 86 this broadcast only when you've previously suspended updates or downloads in order to resume them. 87 It's generally sufficient to simply check for Internet connectivity before beginning an update and, 88 should 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 91 described in the next lesson.</p> 92