Over a million developers have joined DZone.

Android snippet: Check if the device is connected to the internet

DZone's Guide to

Android snippet: Check if the device is connected to the internet

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

Just a quick function to make your live easier :-)

Bart added a comment to the XML + ProgressBar post from yesterday that checks is the device that runs your application has an active internet connection. To make sure its properly searchable i chose to repost it as an Android snippet.

If you guys come across more useful little pieces of code. Just contact me or leave a comment below.

Here is the function

private Boolean isOnline()	{
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni != null && ni.isConnected())
return true;

return false;

 Also don't forget to add the INTERNET (and ACCESS_NETWORK_STATE) permissions to you application :-)

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

.Net developers: use Highcards, the industry's leading interactive charting library, without writing a single line of JavaScript.


Published at DZone with permission of Mark Mooibroek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}