1

Is there any background task which runs with in application to check for internet connectivity for below scenarios as common ?

1.Start of parsing/fetching data from server

2.In between of parsing/fetching from server

Any sample code or links helps me a lot.

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
Manju
  • 720
  • 9
  • 23
  • yes which ever suits best like service or may be receiver i have code which will work only for first scenario – Manju Jan 16 '13 at 12:55

3 Answers3

0

You can use a service for both the tasks.

  • for the first one, invoke the service, when the downloading is going to start.

  • Use a thread in the service that is executed after a while that checks for the active internet connection.

Edit-

It could be something like,

Thread thread = new Thread()
{
      @Override
      public void run() {
          try {
              while(true) {
                  sleep(1000);
                  Toast.makeText(getBaseContext(), "Running Thread...", Toast.LENGTH_LONG).show();
                  // Check internet connectivity here
              }
          } catch (InterruptedException e) {
           Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
          }
      }
  };

thread.start();

Have a look at this post also.

Also I have found two other options,

Have a look at Timer Task and Alarm Manager also to check internet connection after a while.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • thanks for reply but i'm not sure how to do with the thread can you help me with some links or code if possible – Manju Jan 16 '13 at 12:59
  • thanks for your quick response i have one doubt here i will write a service i will place method in that which checks for internet connection i think service will run in background if invoke service before starting of download why i should again write this thread logic as service will always runs in the background some how is not possible to make both in to a single unit sorry if the question which i asked was not correct – Manju Jan 16 '13 at 13:13
  • @ Sahil Mahajan Mj please check the link http://stackoverflow.com/questions/5738419/using-an-android-service-to-handle-a-network-connection which will runs for the first time of application may be you will get idea from this has to work of whole application – Manju Jan 16 '13 at 13:18
  • I just thought of one more option. Async task. Have a look at it. Start the async when the download starts and it will run only when the downloading is in progress. I think this is the best option of all. – Sahil Mahajan Mj Jan 16 '13 at 13:18
0

You can start an IntentService or Service and use this:

public boolean CheckInternet() 
    {
        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.

        if (wifi.isConnected()) {
            return true;
        } else if (mobile.isConnected()) {
            return true;
        }
        return false;
    }

Also don't forget to add this permission to the AndroidManifest file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Rob
  • 455
  • 3
  • 8
  • thanks for reply this will work for first scenario please suggest me if you have any idea which will work for both scenarios thanks in advance – Manju Jan 16 '13 at 13:06
  • just call this method whenever you need it. If you need to check this from multiple classes, you can simply refactor this code into a custom UtilClass as a static method. Before first call to server -> MyUtils.CheckInternet(). After parsing data + before next call to server -> MyUtils.CheckInternet() etc... – Rob Jan 16 '13 at 13:11
0

You can use the below utility class make sure to give internet permission in manifest.

Let me know if any issues.

private static NetworkUtil mInstance;
private volatile boolean mIsOnline;

private NetworkUtil() {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            boolean reachable = false;
            try {
                Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
                int returnVal = process.waitFor();
                reachable = (returnVal==0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            mIsOnline = reachable;
        }
    }, 0, 5, TimeUnit.SECONDS);
}

public static NetworkUtil getInstance() {
    if (mInstance == null) {
        synchronized (NetworkUtil.class) {
            if (mInstance == null) {
                mInstance = new NetworkUtil();
            }
        }
    }
    return mInstance;
}

public boolean isOnline() {
    return mIsOnline;
}
RaghavPai
  • 652
  • 9
  • 14