-1

I called two Web serivces when app started, and those are running in background.

each service is taking about 35-40 seconds approximately. until these service completed i am showing a loader on clicking each Tab, where i have 4 acitvities in my TabActiviy. when ever these services completed i hide the loader and showing the data.

Now,my Question is? while services are running in background by any chance if Network have failed?, how i have to intimate to user by dialogbox that process is interupted by Network.

May i have chance to check network connection by every 10 sec or less in android? so that i can intimate to user with a dialog and resume the services?

Thanks in advance.

3 Answers 3

0

When you use HTTP connection - surround with try-catch block your network data and show error message if some thing happened.

Add Runnable target to your Service

new Runnable() {

    @Override
    public void run() {
        while(true){
            if(socket.isConnected()){

            }else{
                // show error and call dialog
            }
        }
    }
};

if are you working with sockets

0

You could use a Timer and the Android ConnectivityManager:

new Timer().scheduleAtFixedRate(checkCon, 10, 10);

private boolean checkCon() {
  ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

  if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
      &&  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
    return false;
  }
  return true;
}

checkCon() returns false if the Connection to Mobile (conMgr.getNetworkInfo(0)) AND Wifi (conMgr.getNetworkInfo(1)) is disconnected.

1
  • thanks sani this code works excellent for refreshing. now my problem is how to display dialog box when network not available. i am getting problem with this because while webservices are running in background i can navigate from one tab to another tab in my application,as Dialog box in android required context which context i have to send to dialog box where i have 4 tab activities in my tab activity. Nov 14, 2012 at 10:32
0

Have a try of following code .

TimerTask refresher;


                  // Initialization code in onCreate or similar:

                  timer = new Timer();    
                  refresher = new TimerTask() {
                      public void run() {
                          handler.sendEmptyMessage(0);
                      };
                  };
                  // first event immediately,  following after 1 seconds each
                  timer.scheduleAtFixedRate(refresher, 0, 65); 


 @SuppressLint("HandlerLeak")
        final Handler handler = new Handler() {
             public void handleMessage(Message msg) {
                 switch (msg.what) {
                 case REFRESH:


                     break;
                 default:
                     break;
                 }
             }
         };

its worked for me like a charm.. Hope this will help you .

2
  • thanks Rajesh this code works excellent for refreshing. now my problem is how to display dialog box when network not available. i am getting problem with this because while webservices are running in background i can navigate from one tab to another tab in my application,as Dialog box in android required context which context i have to send to dialog box where i have 4 tab activities in my tab activity. Nov 14, 2012 at 10:33
  • hi you can use Broadcast receiver for finding whether network is available or not.. Nov 14, 2012 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.