Click here to Skip to main content
Click here to Skip to main content

Check whether network is available on Android phone

By , 30 Dec 2012
 

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

/**
 * 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)

About the Author

Phat (Phillip) H. VU
Unknown
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5professionalPhat (Phillip) H. VU6 Apr '13 - 22:29 
GeneralHelpfulmemberGremz21 Feb '13 - 15:02 
GeneralMy vote of 5memberMember 190435418 Feb '13 - 23:45 
GeneralMy vote of 5member| B | A | N | D | I |15 Feb '13 - 23:52 
GeneralMy vote of 5memberkksrinivasan8922 Jan '13 - 19:34 
GeneralMy vote of 5memberuyentran311@yahoo.com10 Jan '13 - 14:47 
GeneralRe: My vote of 5memberPhat (Phillip) H. VU10 Jan '13 - 14:55 
GeneralMy vote of 5memberThong Nguyen883 Jan '13 - 14:15 
GeneralRe: My vote of 5memberPhat (Phillip) H. VU10 Jan '13 - 14:55 
GeneralMy vote of 5memberMember 97262843 Jan '13 - 3:55 
GeneralRe: My vote of 5memberPhat (Phillip) H. VU10 Jan '13 - 14:56 
GeneralMy vote of 5memberPhat (Phillip) H. VU30 Dec '12 - 3:17 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 30 Dec 2012
Article Copyright 2012 by Phat (Phillip) H. VU
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid