Determining and Monitoring the Connectivity Status

Source: Internet
Author: User

Some of the most common uses for repeating alarms and background services is to schedule regular updates of application data from Internet resources, cache data, or execute long running downloads. but if you aren't connected to the Internet, or the connection is too slow to complete your download, why both waking the device to schedule the update at all? Http://blog.csdn.net/sergeycao You can use the ConnectivityManager to check that you're actually connected to the Internet, and if so, what type of connection is in place. determine if You Have an Internet ConnectionThere's no need to schedule an update based on an Internet resource if you aren't connected to the Internet. the following snippet shows how to use the ConnectivityManager to query the active network and determine if it has Internet connectivity. connectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm. getActiveNetworkInfo (); boolean isConnected = activeNetwork. isConnectedOrConnecting (); Determine the Type of your Internet ConnectionIt's also possible to determine the type of Internet connection currently available. device connectivity can be provided by mobile data, WiMAX, Wi-Fi, and ethernet connections. by querying the type of the active network, as shown below, you can alter your refresh rate based on the bandwidth available. boolean isWiFi = activeNetwork. getType () = ConnectivityManager. TYPE_WIFI; Mobile data costs tend to be significantly higher than Wi-Fi, so in most cases, your app's update rate shocould be lower when on mobile connections. similarly, downloads of significant size shocould be suincluded until you have a Wi-Fi connection. having disabled your updates, it's important that you listen for changes in connectivity in order to resume them once an Internet connection has been established. monitor for Changes in ConnectivityThe ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net. conn. CONNECTIVITY_CHANGE ") action whenever the connectivity details have changed. you can register a broadcast receiver er in your manifest to listen for these changes and resume (or suspend) your background updates accordingly. <action android: name = "android.net. conn. CONNECTIVITY_CHANGE "/> Changes to a device's connectivity can be very frequent-this broadcast is triggered every time you move between mobile data and Wi-Fi. as a result, it's good practice to monitor this broadcast only when you 've previusly suspended updates or downloads in order to resume them. it's generally sufficient to simply check for Internet connectivity before beginning an update and, shocould there be none, suspend further updates until connectivity is restored.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.