0

Code Snippet:

Handler handler= new Handler();
handler.postDelayed(networkRunnable,
                10000);

/**
 * A runnable will be called after the 10 second interval
 */
Runnable networkRunnable= new Runnable() {
    @Override
    public void run() {
        //Not fired if I quit the app before 10 seconds after 1 second. 
    }
};

Setting Handler post delayed to trigger after 10 seconds. If I quit the app in between 1 to 10 seconds the run method never called.

Please help me on this.

Thanks in advance.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55

2 Answers2

2

The Android runtime aggressively manages process lifetime, destroying processes as their entry points are closed (i.e. when the last activity is finished, for example). Having said that, I don't know of any execution environment in which the code above will execute the callback reliably without additional logic.

If you want the call-out to fire for sure, you will need to register a Service with the Android core and use the service thread's Handler to schedule the call-out. Android will (usually) keep the Service running and your call-out will be fired later. You should then also unregister the service in the call-out to free up system resources.

Mark McKenna
  • 2,857
  • 1
  • 17
  • 17
  • Checked by starting the intent service from my alarm receiver. This also not working because I am setting up the 15 and 10 seconds handler post delayed and all. So before to that intent service called out. So what about trying with android servces. – M Vignesh May 23 '15 at 15:52
  • @MVignesh I'm not sure I understand what you're saying. Are you starting a service in response to an alarm which holds onto the 10 second call-out, and the call-out is still not firing? – Mark McKenna May 23 '15 at 16:18
  • Inside of Alarm receiver I am going to start the service. The service code having 10 second post delayed handler. Is than fine? – M Vignesh May 23 '15 at 18:26
  • And what will happen calling the same service again and again immediately. – M Vignesh May 23 '15 at 18:26
  • Use the Singleton pattern for your service--ensure that you obtain a handle to your service first, and only instantiate the service if the handle is not already populated. Keep track of how many outstanding call-outs you have, and only close the service after that counter goes to zero. Finally, you need a synchronization block which ensures that no invocations against your service can occur while the counter is being serviced--that is, the counter doesn't go back up to nonzero after it has reached zero, and no calls hook a dying service instance. Getting this right is not easy. – Mark McKenna May 24 '15 at 03:21
  • Thanks for the answer. I have implemented it in a separate service. – M Vignesh May 27 '15 at 12:46
0

In order to achieve this I used an AlarmManager instead of the handler at, its working now, this is an extract of the code I'm using:

@Override   
protected void onHandleIntent(Intent intent) {
    Log.d(Constants.TAG, "onHandleIntent");
    ...
    restartService();
    Log.d(Constants.TAG, "finish");   
}

private void restartService() {
   AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
   Intent queryIntent = new Intent(context, ServiceClass.class);
   PendingIntent pendingQueryIntent = PendingIntent.getService(context, 0, queryIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);
    // schedule the intent for future delivery
    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + Constants.RESTART_TIME, pendingQueryIntent);
}

In this way I'm able to restart the service no matter if the user closes the app using home, back or swipe from recent apps, the only way it stops working is if the uses forces the app to stop.

Hope it helps

JavierSP1209
  • 899
  • 8
  • 17