Android Network General class Netutil

Source: Internet
Author: User

1. Overall analysis

1.1. The source code is as follows, you can copy directly.

 Public classNetutil {/*** The user is connected to the network * *@paramContext Context*/     Public Static Booleanisnetconnection (Context context) {FinalConnectivitymanager Connectivitymanager =(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); FinalNetworkinfo Networkinfo =Connectivitymanager.getactivenetworkinfo (); return(Networkinfo! =NULL&&networkinfo.isavailable ()); }    /*** Whether to connect to WiFi * *@paramContext Context*/     Public Static Booleaniswificonnection (Context context) {Connectivitymanager Connectivitymanager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo Wifinetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); if(wifinetworkinfo.isconnected ()) {return true; }        return false; }}
View Code

1.2. Main methods.

    • Determine if your phone is connected to the network
    • Determine if the phone is connected to WiFi

There are many other methods that you can use to add again.


2. Local analysis

2.1. Determine if the phone is connected to the network

  

Here's a system service, Context.getsystemservice (context.connectivity_service) Get manager

Then use an instance of the manager. Getactivenetworkinfo () to get network connection status information.

2.2. Determine if your phone is connected to WiFi

  

Judging WiFi, in fact, is based on the former, with the manager. Getnetworkinfo (the status code of WiFi is inside)

In fact, the general situation to determine whether the network exists or not, because the network includes mobile phone data connection +wifi.



Android Network General class Netutil

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.