Android Official Demo Basicnetworking

Source: Internet
Author: User

This example demonstrates how to use the Android API to check for network connections.

demo:https://github.com/googlesamples/android-basicnetworking/#readme

Related api:https://developer.android.google.cn/reference/android/net/connectivitymanager.html

Use Connectivitymanager to check if the network is connected, and if so, determine the network type. Gets the Networkinfo object through the Connectivitymanager.getactivenetworkinfo () method to obtain network state information.

Key code:

/** * Check if the network is connected,If connected, determine if WiFi status or other network type.*/private void Checknetworkconnection () {Connectivitymanager connmgr=(Connectivitymanager) getsystemservice (context.connectivity_service);Networkinfo activeinfo = Connmgr.getactivenetworkinfo ();if (activeinfo! =null && activeinfo.isconnected ()) {wificonnected= Activeinfo.gettype () = = Connectivitymanager.type_wifi;mobileconnected = Activeinfo.gettype () = = Connectivitymanager.type_mobile;if (wificonnected) {log.i (TAG, GetString (r.string.wifi_connection));} else if (mobileconnected) {log.i (TAG, GetString (r.string.mobile_connection));}} else {log.i (TAG, GetString (R.string.no_wifi_or_mobile));    }}

Android Official Demo Basicnetworking

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.