Check Internet connectivity with your own jar file in Android

Hi Android lovers… 🙂 After a long time. You can remember our last posts. Those are about connecting Internet and handling HTTP GET and POST methods. But have you ever consider to check whether your device is connected to Internet or not. Because, by the time your application runs, if the device is not connected to the Internet, you will get an error. As programmers, our duty is to check the infrastructure before user runs the application and get any errors. The important thing in this post is we are going to use our own jar file to check Internet connectivity 🙂

Coding wise this is a very small blog post. There are no UIs (User Interfaces)  in this post. What you can see as the out put when you run this program is a Toast message. If your device is connected to any network, it will show you the message that you are “Connected” and the type of the network, that is MOBILE or WIFI. If not, it will show you “NOT connected” message.

Before I am going to drop the source code, I want to show you how to add external jar files in to your project.

  • Create a directory called “libs” under your root directory
  • Copy your jar file in to that “libs” directory
  • Expand the “libs” directory in your Project Explorer window
  • Right click on the jar file -> select Build Path -> select Add To Build Path

From this link you can download my tiny jar file that I am going to use in the following source code.

package lk.anujarosha.checkinternet;

 * @author AnujAroshA
 * Give following permission in the Manifest
 * android.permission.ACCESS_NETWORK_STATE

import com.anuja.chkinternet.CheckInternet;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CheckInternetUsingJarActivity extends Activity {

	private static final String TAG = "CheckInternetUsingJarActivity";
	private ConnectivityManager connectivityManager;
	private CheckInternet checkInternet;
	private Context context;
	private boolean connectivityState;
	private String connectedNetworkType;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        Log.i(TAG, "* onCreate");

        connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        checkInternet = new CheckInternet(context);

        connectivityState = checkInternet.isConnected(connectivityManager);

        	connectedNetworkType = checkInternet.getNetworkType();

        	Toast.makeText(getApplicationContext(), "Connected via : " + connectedNetworkType, Toast.LENGTH_LONG).show();
        	Toast.makeText(getApplicationContext(), "NOT connected", Toast.LENGTH_LONG).show();

That’s all about the code. As I said it is a very little piece of code.  CheckInternet is not a default JAVA class. That is the class I have used in my jar file. You can clearly understand when you see the related documentation for that jar file that I have uploaded.

Thanks for staying with me 🙂 Will meet with something different in next time. Till then Bye for all 😉


About AnujAroshA

Working as a Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in Android Examples and tagged , . Bookmark the permalink.

3 Responses to Check Internet connectivity with your own jar file in Android

  1. sachin says:

    This code working fine for me in the emulator but it is not working with real device. what to do now?

    • anujarosha says:

      Hi Sachin,

      How that can be? :-/ I have checked with real devices also and it is working for me. Can’t think exactly. Hummm… please check the attributes minSdkVersion and targetSdkVersion in your Manifest is compatible with your device.

  2. bro…thanks alllot….no worries……everythin is fine n fluent workin n also i want you to include progress dialog so that the the demo would be awesome….excellent job…..believe it or not….i had spent nearly 3 months just searchin for this kinda code…:)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s