Monitoring of Android network connection status

Source: Internet
Author: User

Some applications need to connect to the network, such as updating the backend service, refreshing the data, etc., the most common practice is to regularly network, directly using online resources, cache data or perform a download task to update the data.

But if the terminal is not connected to the network, or the speed is slow, there is no need to perform these tasks. You can use Connectivitymanager to check what the Internet is and what type of network is currently. Specific

The code is as follows:

    Connectivitymanager cm =            (connectivitymanager) Getapplicationcontext (). Getsystemservice ( Context.connectivity_service);     =  cm.getactivenetworkinfo ();     Boolean // whether to connect to the network    boolean Iswifi = activenetwork.gettype () = = Connectivitymanager.type_wifi;

Mobile networks consume more power than Wi-Fi, so in most cases apps should reduce the frequency of data refresh when they have a mobile network, while downloading large files while in the WiFi state. When the network is reconnected,

If you find that the current status is WiFi status, you can restart the previous stop download operation or refresh operation. Network connection state switching can be done by registering a broadcast receiver to the network status

Listening.

<android:name= "Android.net.conn.CONNECTIVITY_CHANGE"/>

Monitoring of Android network connection status

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.