0

I have an application which needs to communicate with the server at some random interval through GPRS or EDGE.. But there are few possibilities at which internet cannot be accessed by the application when the user is in call or deactivated etc. At these time i have two scenario's recoverable and non recoverable.

Recoverable scenarios

  • On phone call ( User will hang up and data connection will be active again)
  • No Signal (Sometimes signal may drop and the phone will get signal again)

Non Recoverable Scenarios

  • Flight mode
  • Deactivating Data Connection

When its recoverable i can try again for the connection after some defined interval. And during non recoverable i have to alert user. For instance if the user deactivates data connection or enables flight mode i have to alert the user.

EDIT:I can able to detect flight mode through one of the intents. I couldn't able to find for others.

Jeyanth Kumar
  • 1,589
  • 3
  • 23
  • 48

2 Answers2

1

The below code return if valid connections are available

public boolean isConnectionsAvailable() {
         boolean lRet = false;
         try{
             ConnectivityManager conMgr =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
             NetworkInfo info= conMgr.getActiveNetworkInfo();  
             if(info != null && info.isConnected()) {  
                 lRet = true ;
             }else{  
                 lRet = false ;
                }
         }catch (Exception e) {
             Log.d("Connection Error", e.toString());
             lRet = false ;
        }
         return lRet;
       }

After this, if you have low signal strength then you make a HTTP request by setting relevant time out to it. If timeout happened give relevant alert msg to user as below

public void serverCall(String pURL){

   if (isConnectionsAvailable()){
     // Call server by setting proper timeout

   }

}

Edit:

To check the Airplane mode status:

private static boolean isAirplaneModeOn(Context context) {

   return Settings.System.getInt(context.getContentResolver(),
           Settings.System.AIRPLANE_MODE_ON, 0) != 0;

}
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
  • i can check if data connection is available or not.. i need to know the reason for no connection so that application and wait and try or alert the user... – Jeyanth Kumar Apr 20 '12 at 06:48
  • Check I have edit my answer, I have provided one of the reason for no connectivity which checks the airplane mode status. There are other techniques to check different reasons for no connectivity.. – Vinayak Bevinakatti Apr 20 '12 at 07:23
  • thanks dude.. but i use an intent to check the flight mode status. which i can use to alert the user immediately when user turns on flight mode.. i just for scenarios like low signal, data connection deactivation etc – Jeyanth Kumar Apr 20 '12 at 09:55
0

You could try surrounding your method with try and catch. If the method fails because it cannot connect to your server for whatever reason you could call postDelayed from a Handler and re-run your method again in a pre-determined length of time.

Handler mHandler = new Handler();
Runnable yourMethodRunnable = new Runnable(){
    @Override
    public void run(){
        yourMethod();
    }
};

private void yourMethod(){
    try{
        // talk to server
    } catch (InCallException e) {
        mHandler.postDelayed(yourMethodRunnable, delay)
    } catch (NoSignalException e) {
        // etc...
    } catch (OtherException e) {
        // etc...
    }
}

The exceptions are just examples, and likely don't exist, get the exceptions that you want to catch either from the Android Developer Docs, or by looking at the output from LogCat when you re-enact each time that the connection to the server would fail.

TerryProbert
  • 1,124
  • 2
  • 10
  • 28