Android Learning note-detects if it's a WiFi environment

Source: Internet
Author: User

Connectivitymanager cm = (Connectivitymanager) getsystemservice (Context.connectivity_service);

Get the Networkinfo object that represents the networked state

Networkinfo wifiinfo = Cm.getnetworkinfo (Connectivitymanager.type_wifi);

return wifiinfo! = null && wifiinfo.isconnected ()

True is the WiFi environment, and can be used normally, otherwise WiFi is not available

Connectivitymanager main management and network connection related operations

The Networkinfo class contains a detailed description of the WiFi and mobile two network mode connections, and the state object obtained through its GetState () method represents

The status of the connection success or not.

    1. /Jump to the wireless network Settings screen
    2. StartActivity (new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    3. //Jump to the unlimited WiFi network Settings screen
    4. StartActivity (new Intent (Android.provider.Settings.ACTION_WIFI_SETTINGS));

Context context = Mainactivity.getapplicationcontext ();//Get Application context
Connectivitymanager Connectivitymanager = ( Connectivitymanager) Context
. Getsystemservice (Context.connectivity_service);//Get the system's connection service
Networkinfo Networkinfo = Connectivitymanager.getactivenetworkinfo ();//Gets the connection condition of the network
if ( networkinfo.gettype () = = Connectivitymanager.type_wifi) {
//determine WIFI network
}else if ( networkinfo.gettype () ==connectivitymanager.type_ MOBILE) {
//judging 3G net
}

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.