Scott Main | 604e4ed | 2011-12-13 18:24:34 -0800 | [diff] [blame] | 1 | page.title=Determining and Monitoring the Connectivity Status |
Scott Main | 580f014 | 2011-12-15 16:47:26 -0800 | [diff] [blame] | 2 | parent.title=Optimizing Battery Life |
Scott Main | 604e4ed | 2011-12-13 18:24:34 -0800 | [diff] [blame] | 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/topics/intents/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.isConnectedOrConnecting();</pre> |
| 53 | |
| 54 | |
| 55 | <h2 id="DetermineType">Determine the Type of your Internet Connection</h2> |
| 56 | |
| 57 | <p>It's also possible to determine the type of Internet connection currently available.</p> |
| 58 | |
| 59 | <p>Device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. By |
| 60 | querying the type of the active network, as shown below, you can alter your refresh rate based on |
| 61 | the bandwidth available.</p> |
| 62 | |
| 63 | <pre>boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;</pre> |
| 64 | |
| 65 | <p>Mobile data costs tend to be significantly higher than Wi-Fi, so in most cases, your app's update |
| 66 | rate should be lower when on mobile connections. Similarly, downloads of significant size should be |
| 67 | suspended until you have a Wi-Fi connection.</p> |
| 68 | |
| 69 | <p>Having disabled your updates, it's important that you listen for changes in connectivity in order |
| 70 | to resume them once an Internet connection has been established.</p> |
| 71 | |
| 72 | |
| 73 | <h2 id="MonitorChanges">Monitor for Changes in Connectivity</h2> |
| 74 | |
| 75 | <p>The {@link android.net.ConnectivityManager} broadcasts the {@link |
| 76 | android.net.ConnectivityManager#CONNECTIVITY_ACTION} ({@code |
| 77 | "android.net.conn.CONNECTIVITY_CHANGE"}) action whenever the connectivity details have changed. You |
| 78 | can register a broadcast receiver in your manifest to listen for these changes and resume (or |
| 79 | suspend) your background updates accordingly.</p> |
| 80 | |
| 81 | <pre><action android:name="android.net.conn.CONNECTIVITY_CHANGE"/></pre> |
| 82 | |
| 83 | <p>Changes to a device's connectivity can be very frequent—this broadcast is triggered |
| 84 | every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor |
| 85 | this broadcast only when you've previously suspended updates or downloads in order to resume them. |
| 86 | It's generally sufficient to simply check for Internet connectivity before beginning an update and, |
| 87 | should there be none, suspend further updates until connectivity is restored.</p> |
| 88 | |
| 89 | <p>This technique requires toggling broadcast receivers you've declard in the manifest, which is |
| 90 | described in the next lesson.</p> |