Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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" />

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

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.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}