Android Determines network connection status

Source: Internet
Author: User

Sometimes our application may need to determine if the current device is networked

1 Private voidinit () {2         /**get system-level networking administrator Objects*/3Connectivitymanager manager = (Connectivitymanager) This4 . Getsystemservice (connectivity_service);5Networkinfo info =manager.getactivenetworkinfo ();6         if(Info = =NULL) {//without the network condition7             //jump to the Network Settings page8StartActivity (NewIntent (Android.provider.Settings.ACTION_SETTINGS));9}Else{//In the case of a networkTen             if(Info.isavailable ()) {//when the network is available One                 /**It's the way the phone comes online.*/ A                 if(Info.gettype () = =connectivitymanager.type_mobile) { -                     if(info.getstate () = =state.connected) { - Toast.maketext ( theMainactivity. This, -"Mobile network is available and connected" + "\ n" + "connect network as: \ n" -+ info.gettype () + ", MOBILE", 1). Show (); -                     } +                 /**WiFi connection Mode*/ -}Else { +Toast.maketext (mainactivity. This, A"Connection Network mode:" + info.gettype () + ", Wi-Fi", 1). Show (); atStartActivity (NewIntent ( - Android.provider.Settings.ACTION_WIFI_SETTINGS)); -                 } -}Else { -Toast.maketext (mainactivity. This, "Mobile phone network unavailable", 1). Show (); -             } in         } -}

However, finally note that the following permissions are added to allow the program to access information about GSM network (allows applications to accessinformation about networks), otherwise it will be an error

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

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.