How can I determine whether Android phones are connected to the Internet ?, Android current

Source: Internet
Author: User

How can I determine whether Android phones are connected to the Internet ?, Android current

If you want to develop a network application program, first consider whether to connect to the network and determine whether the network can pass through the Android mobile phone.

ConnectivityManager class isAvailable () method judgment,

First, obtain the network communication instance:

Java code  
  1. ConnectivityManager cwjManager = (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );

 

, Use

Java code  
  1. CwjManager. getActiveNetworkInfo (). isAvailable ();

 

If it is True, it indicates that the current Android phone is connected to the Internet, which may be WiFi, GPRS, HSDPA, etc,

You can use the getActiveNetworkInfo () method of the ConnectivityManager class to determine the detailed access method,

Note that you need to add

Xml Code  
  1. <Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>

 

This permission, android Development Network reminds everyone to use this method in both the Market and Browser programs on the real machine to determine whether to continue, when some network times out, you can also check whether the network connection exists to avoid wasting the power resources on the mobile phone.

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.