Android detects network connection status

Source: Internet
Author: User

Android detects network connection status

When the Android APP needs to connect to the network, it does not connect to the network every time. Therefore, you need to check the network status of the current device in the program so that users can be reminded in a timely manner.

 

To determine the network status, you must have the following permission code (AndroidManifest. xml ):

 

  
   
  
 

Code used to check the current network status:

 

Public void checkNetworkState (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = mConnectivityManager. getActiveNetworkInfo (); // 1. determine whether there is a network connection boolean networkAvailable = networkInfo. isAvailable (); // 2. obtains the type of the current network connection. int networkType = networkInfo. getType (); if (ConnectivityManager. TYPE_WIFI = networkType) {// currently wifi network} else if (ConnectivityManager. TYPE_MOBILE = networkType) {// currently mobile network }}}



 


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.