WIFI/3G Network connection in Android

Source: Internet
Author: User
Tags tostring

Get Network Connection Status

With the spread of 3G and WiFi, more and more Android applications need to invoke network resources, and detection of network connectivity is a necessary feature of network applications.

The Android platform provides a Connectivitymanager class for detection of network connectivity status.

The Android development document describes Connectivitymanager's role in this way:

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get a instance of this class by calling

Context.getsystemservice (Context.connectivity_service).

The primary responsibilities of this class are to:

Monitor Network Connections (Wi-Fi, GPRS, UMTS, etc.)

Send broadcast intents when network connectivity changes

Attempt to ' fail over ' to another network when connectivity to a network is lost

Provide an API this allows applications to query the coarse-grained or fine-grained state of the available

The following simple example checknetworkinfo () shows how to programmatically get the current network status of the Android phone

private void Checknetworkinfo ()  
   {  
       Connectivitymanager conman = (Connectivitymanager) getsystemservice

( Context.connectivity_service);  
      
       Mobile 3G Data Network state  
       mobile = Conman.getnetworkinfo (Connectivitymanager.type_mobile). GetState ();  
       Txt3g.settext (Mobile.tostring ()); Show 3G network connection status  
       //wifi state  
       WiFi = Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();  
       Txtwifi.settext (Wifi.tostring ()); Show WiFi connection Status  
   }

Note:

According to the Android security mechanism, when using Connectivitymanager, you must add the <uses-permission in the Androidmanifest.xml

Android:name= "Android.permission.ACCESS_NETWORK_STATE"/> cannot obtain permission from the system.

Run result (turn off 3G and WiFi network connection state)

Call the Android phone's network Configuration interface

Use the mobile phone QQ friends, you should know, when the QQ started, if there is no effective network connection, QQ will prompt into the mobile phone network configuration interface. How did this happen?

private void Checknetworkinfo ()  
    {  
        Connectivitymanager conman = (Connectivitymanager) getsystemservice

( Context.connectivity_service);  
      
        Mobile 3G Data Network state  
        mobile = Conman.getnetworkinfo (Connectivitymanager.type_mobile). GetState ();  
        Txt3g.settext (Mobile.tostring ());  
        WiFi state  
        WiFi = Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();  
        Txtwifi.settext (Wifi.tostring ());  
              
        If the 3G network and WiFi network are not connected and are not in a connected state, enter the network setting interface by user-configured network connection if  
        (mobile==state.connected| | mobile==state.connecting) return  
            ;  
        if (wifi==state.connected| | wifi==state.connecting) return  
            ;  
              
              
        StartActivity (New Intent (settings.action_wireless_settings));//Enter the wireless network Configuration Interface  
        //startactivity (New Intent ( settings.action_wifi_settings)); Access to the WiFi network Setup interface in your phone  
    

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.