Network:android Network Judgment (WiFi, 3G and others)

Source: Internet
Author: User

public class Networkprober {

/**
* Whether the network is available
*
* @param activity
* @return
*/
public static Boolean isnetworkavailable (context context) {
Connectivitymanager connectivity = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
if (connectivity = = NULL) {
} else {
Networkinfo[] info = Connectivity.getallnetworkinfo ();
if (info! = null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}

/**
* Whether the GPS is turned on
*
* @param context
* @return
*/
public static Boolean isgpsenabled (context context) {
Locationmanager Locationmanager = ((Locationmanager) context
. Getsystemservice (Context.location_service));
list<string> accessibleproviders = Locationmanager.getproviders (true);
return accessibleproviders! = null && accessibleproviders.size () > 0;
}

/**
* WiFi is turned on
*/
public static Boolean iswifienabled (context context) {
Connectivitymanager mgrconn = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Telephonymanager Mgrtel = (telephonymanager) context
. Getsystemservice (Context.telephony_service);
Return ((Mgrconn.getactivenetworkinfo ()! = null && mgrconn
. Getactivenetworkinfo (). GetState () = = NetworkInfo.State.CONNECTED) | | Mgrtel
. Getnetworktype () = = Telephonymanager.network_type_umts);
}

/**
* Determine if the current network is a WiFi network
* IF (Activenetinfo.gettype () ==connectivitymanager.type_mobile) {//Judgment 3G Net
*
* @param context
* @return Boolean
*/
public static Boolean Iswifi (context context) {
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo activenetinfo = Connectivitymanager.getactivenetworkinfo ();
if (activenetinfo! = null
&& Activenetinfo.gettype () = = Connectivitymanager.type_wifi) {
return true;
}
return false;
}

/**
* Determine if the current network is a 3G network
*
* @param context
* @return Boolean
*/
public static Boolean is3g (context context) {
Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo activenetinfo = Connectivitymanager.getactivenetworkinfo ();
if (activenetinfo! = null
&& Activenetinfo.gettype () = = Connectivitymanager.type_mobile) {
return true;
}
return false;
}
}
There are also two ways to determine if a network is available:

public static Boolean isnetworkavailable_00 (context context) {
Connectivitymanager cm = ((Connectivitymanager) context
. Getsystemservice (Context.connectivity_service));
if (cm! = null) {
Networkinfo info = Cm.getactivenetworkinfo ();
if (info! = null && info.isconnectedorconnecting ()) {
return true;
}
}
return false;
}

public static Boolean isnetworkavailable_01 (context context) {
Connectivitymanager cm = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo network = Cm.getactivenetworkinfo ();
if (network! = null) {
return network.isavailable ();
}
return false;
}
A more rigorous notation:
public static Boolean checknet (context context) {

try {
Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
if (connectivity! = NULL) {

Networkinfo info = Connectivity.getactivenetworkinfo ();
if (info! = null && info.isconnected ()) {

if (info.getstate () = = NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}

Network:android Network Judgment (WiFi, 3G and others)

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.