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