Network State Acquisition

Source: Internet
Author: User

    1. Need to configure permissions

To Add code in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

2. Use the Connectivitymanager class to encode operations.

Public void getnetworkstate ()  {    connectivitymanager connmanager  =  (Connectivitymanager)  this.getsystemservice (connectivity_service);     //   Get Networkinfo objects that represent the networked state     NetworkInfo networkInfo =  Connmanager.getactivenetworkinfo ();    //  gets whether the current network connection is available     if   (Null == networkinfo)  {        toast.maketext ( this,  "Current network connection is not available",  toast.length_short). Show ();         // When the network is unavailable, jump to the Network Settings page         startactivityforresult (new intent ( Android.provider.Settings.ACTION_WIRELESS_SETTINGS),  1);    } else {         boolean available = networkinfo.isavailable ();         if (available)  {            log.i ("Notification",  " The current network connection is available ");             toast.maketext (this,  "Current network connection available",  toast.length_short). Show ();        } else  {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("notification",  "Current network connection is not available");             toast.maketext (this,  "Current network connection is not available",  toast.length_short). Show ();         }    }     networkinfo.state state = connmanager.getnetworkinfo ( Connectivitymanager.type_mobile). GetState ();    if  (networkinfo.state.connected  == state)  {        log.i ("Notification",  "GPRS network is connected");         Toast.maketext (this,  "GPRS network is connected",  toast.length_short). Show ();    }     state = connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();     if  (networkinfo.state.connected == state)  {     &NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("Notification",  "WiFi network is connected");         Toast.maketext (this,  "WiFi network is Connected",  toast.length_short). Show ();    }     ////  Jump to wireless Network Setup interface     // startactivity (new    //  intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS));    ////  Jump to the unlimited WiFi network Settings Interface     // startactivity (New    // intent ( Android.provider.Settings.ACTION_WIFI_SETTINGS));}

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" margin:0px;padding:0px ; border:none; "/>


Network State Acquisition

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.