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>