"Android" detailed Android network operation

Source: Internet
Author: User

Directory structure:

contents Structure [+]
    1. Judging the network
      1. Determine if there is an Internet connection
      2. Determine if a WiFi network is available
      3. Determine if the mobile network is available
      4. Gets the type information for the current network connection
    2. Listening Network

Obtaining network information requires that the appropriate permissions be added to the Androidmanifest.xml file.

<android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
1. Determine network 1.1 to determine if there is an Internet connection
 Public Boolean isnetworkconnected (Context context) {ifnull==  Mconnectivitymanager.getactivenetworkinfo (); if NULL ) {return  mnetworkinfo.isavailable ();}} return false ;}
1.2 Determining if a WiFi network is available
 public  boolean   iswificonnected (context context) { if  (context! = null  ) {Connectivitymanager Mconnectivitymanager  = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo mwifinetworkinfo  = Mconnectivitymanager.getnetworkinfo ( Connectivitymanager.type_wifi);  if  (Mwifinetworkinfo! = null   return   Mwifinetworkinfo.isavailable ();}}  return  false  ;}  

It is important to note that this method only determines whether WiFi is available in the current environment, not whether it is connected to Wi-Fi.

1.3 Determine if the mobile network is available
 public  boolean   ismobileconnected (context context) { if  (context! = null  ) {Connectivitymanager Mconnectivitymanager  = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo mmobilenetworkinfo  = Mconnectivitymanager.getnetworkinfo ( Connectivitymanager.type_mobile);  if  (Mmobilenetworkinfo! = null   return   Mmobilenetworkinfo.isavailable ();}}  return  false  ;}  
1.4 Get the type information for the current network connection
 public  static  int   Getconnectedtype (context context) { if  (Context! = null   = (Connectivitymanager) Context.getsystemservice ( Context.connectivity_service); Networkinfo mnetworkinfo  = Mconnectivitymanager.getactivenetworkinfo ();  if  (Mnetworkinfo! = null  && return   Mnetworkinfo.gettype ();}}  return  -1 

The return value of GetType () can be type_mobile, Type_wifi, Type_wimax, Type_ethernet, Type_bluetooth, and other types in Connectivitymanager.

2. Listening Network

Define the broadcast recipient:

Broadcastreceiver Connectionreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {Connectivitymanager Mconnectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Mnetworkinfo=Mconnectivitymanager. Getactivenetworkinfo (); intType =-1; if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) {Type=Mnetworkinfo.gettype (); }            if(Type = =Connectivitymanager.type_wifi) {Toast.maketext (mainactivity). This, "WiFi", Toast.length_short). Show (); } Else if(Type = =connectivitymanager.type_mobile) {Toast.maketext (mainactivity). This, "Network Available", Toast.length_short). Show (); }Else if(Type==-1) {Toast.maketext (mainactivity. This, "Network Unavailable", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Unknown Network", Toast.length_short). Show (); }        }    };

Register at the right place:

        New Intentfilter ();        Intentfilter.addaction (connectivitymanager.connectivity_action);        Registerreceiver (Connectionreceiver, intentfilter);

To cancel the registration in the appropriate location:

Unregisterreceiver (Connectionreceiver);

"Android" detailed Android network operation

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.