[Android development FAQ-6] How can I check whether the current network of a mobile phone is available?

Source: Internet
Author: User

Android applications connected to IOT generally start with the welcome interface to check the network status and synchronize the background server. If the network status is unavailable, the user must be notified that the current network is unavailable, the following describes how to implement this function.

Public static Boolean isnetworkavailable (context) {connectivitymanager connect = (connectivitymanager) context. getsystemservice (context. connectivity_service); If (connect = NULL) return false; networkinfo netinfo = connect. getactivenetworkinfo (); If (netinfo = NULL) return false; If (netinfo. isconnected () return true; return false ;}

Add the following permissions to manifest. xml:

<Uses-Permission Android: Name = "android. Permission. Internet"/> <uses-Permission Android: Name = "android. Permission. access_network_state"/>

CodeVery easy to understand ~

Note: It is best to open a new thread when checking or synchronizing data in the background, otherwise the interface will be stuck easily.

For multithreading development, refer to the blog:Http://blog.csdn.net/shishengshi/article/details/9182855

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.