A summary of Android network judgment Knowledge _android

Source: Internet
Author: User

Android to determine if the current network is available

Application scenario: Implementation to determine whether the current network is available

There are currently available networks, as shown below:

There are currently no networks available, as shown in the following figure:

Implementation steps:

1. Get Connectivitymanager Object

Context context = Activity.getapplicationcontext ();
Get all connection management objects on the phone (including the management of connections to wi-fi,net)
connectivitymanager Connectivitymanager = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service);

2. Get Networkinfo Object

Get Networkinfo object
networkinfo[] Networkinfo = Connectivitymanager.getallnetworkinfo ();

3, determine whether the current network state is connected state

if (networkinfo[i].getstate () = = NetworkInfo.State.CONNECTED)
{return
   true;
}

4. Add access to current network state permissions in Androidmanifest.xml

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

The code is as follows:

public class Classtestdemoactivity extends activity {@Override public void onCreate (Bundle savedinstancestate) {
    Super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main); if (isnetworkavailable (classtestdemoactivity.this)) {Toast.maketext (Getapplicationcontext (), "There is currently a network available!")
    ", Toast.length_long). Show (); else {toast.maketext (getapplicationcontext), "There is no network currently available!"
    ", Toast.length_long). Show (); }/** * Check that the current network is available * * @param context * @return/public boolean isnetworkavailable (active ACTI
    Vity) {Context context = Activity.getapplicationcontext (); Get all connection management objects on the phone (including the management of connections to wi-fi,net) Connectivitymanager Connectivitymanager = (Connectivitymanager)
    Context.getsystemservice (Context.connectivity_service);
    if (Connectivitymanager = = null) {return false;
      else {//Get Networkinfo object networkinfo[] Networkinfo = Connectivitymanager.getallnetworkinfo (); if (Networkinfo!= null && networkinfo.length > 0 {for (int i = 0; i < networkinfo.length; i++)
          {System.out.println (i + "= = = State = =" + networkinfo[i].getstate ());
          SYSTEM.OUT.PRINTLN (i + "= = = Type = = =" + Networkinfo[i].gettypename ()); Determines whether the current network state is a connection state if (networkinfo[i].getstate () = = NetworkInfo.State.CONNECTED) {return
          True
  }}} return false;

 }
}

The result of the console printing:

Android about determining whether an application has a network

In Android programming, there are many applications to determine whether there is a network of problems, now I would like to say about the network, the source code is as follows:

. java

 /** * Check whether the current network is available * * @param context * @return/public boolean isnetworkavailable (active activity)
    {Context context = Activity.getapplicationcontext (); Get all connection management objects on the phone (including the management of connections to wi-fi,net) Connectivitymanager Connectivitymanager = (Connectivitymanager)
    Context.getsystemservice (Context.connectivity_service);
    if (Connectivitymanager = = null) {return false;
      else {//Get Networkinfo object networkinfo[] Networkinfo = Connectivitymanager.getallnetworkinfo ();  if (networkinfo!= null && networkinfo.length > 0) {for (int i = 0; i < networkinfo.length;
          i++) {System.out.println (i + "= = = State = =" + networkinfo[i].getstate ());
          SYSTEM.OUT.PRINTLN (i + "= = = Type = = =" + Networkinfo[i].gettypename ()); Determines whether the current network state is a connection state if (networkinfo[i].getstate () = = NetworkInfo.State.CONNECTED) {return
          True
   }
        }
      }
    } return false; }

To judge this method in the OnCreate () method, the specific code is as follows:

. java

@Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);  
    Setcontentview (r.layout.activity_main);
    if (isnetworkavailable (maintivity.this) {
 Toast.maketext (this, "There is currently a network available!") ", Toast.length_long). Show ();
 else{
 Toast.maketext (This, "No network currently!") ", Toast.length_long). Show ();}
 

Additionally, you need to add permissions

<!--allows applications to be networked-->
  <uses-permission android:name= "Android.permission.INTERNET"/>
   <!-- Allow applications to check network conditions-->
 <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission>

Related Article

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.