Click here to Skip to main content
15,867,835 members
Articles / Mobile Apps / Android
Tip/Trick

Check whether network is available on Android phone

Rate me:
Please Sign up or sign in to vote.
4.92/5 (32 votes)
30 Dec 2012CPOL 41.3K   157   16
This code snippet checks whether network is available on an Android phone.

Introduction

In some Android applications, they can be run properly once the network on device is enable. Or, Android developers want to check the network status before proceed. The simple code snippet I will show today is so simple but sometime it will take you a little bit of time to find/research. 

This routine was run and tested as well on Android 2.2 (API 8) 

Using the code

Java
/**
 * Checks whether the network is available on Android device.
 * If the network signal is very low, it will be evaluated as NOT available.
 * This routine will check both MOBILE and WIFI signal.
 * If both of them are in disable status, <code>false</code> will be return absolutely.
 * </p>
 * Return <code>true</code> if the network is available. Otherwise, return <code>false</code>.
 * </p>
 * 
 * @param ctx Context.
 * 
 * @return <code>True</code> : the network is available.</br>
 *         <code>False</code>: the network is NOT available.
 */
 public static boolean isNetworkAvailable(Context ctx) {
	ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
	if(connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() ||
		connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){
			return true;
	}
		
	return false;
 } 

History

First release on Dec 30, 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Bandi Ramesh15-Feb-13 23:52
Bandi Ramesh15-Feb-13 23:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.