0

I have used 20 seconds Handler Post Delayed Timer task inside Alarm Receiver on-receive Method. The app is working fine if i quit the app before the alarm receiver on-receive method gets called. If I quit the app once the Handler Post delayed Timer task scheduled. Then the Handler Post delayed Timer task automatically cancelled after quitting the application. So post delayed Timer task never called in my application.

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() {
        // Called after 10 seconds
        cancelNetworkTask();
        // My Job to do after 10 seconds
    }
};

After quitting the app then from when next alarm receiver on-receive method called will schedule the timer task and is working fine.

I tried goAsync() inside of Alarm Receiver on-receive method. So this also not helps me to solve this issue. Once i quit the application my scheduled timer task is cancelled.

How to keep the scheduled handler post delayed timer task is available once the app quits inside of Alarm Receiver on-receive Method.

Please help me on this.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55
  • Start a service from onReceive method and there start timertask – Praveena Jan 22 '15 at 06:01
  • Application Alarm Manager will be started when the intent is fired even if app has been closed. My problem is When I close my app after the timer task has been scheduled, the scheduled timer task is cancelled. – M Vignesh Jan 22 '15 at 07:35
  • thats obvious.. because timer task is not a background process – Praveena Jan 22 '15 at 07:39
  • Instead of using timer task i have user handler post delayed. My app running fine even after the app quits. But once scheduled the post delayed, then I quit my application will cancel the post delayed. – M Vignesh Jan 22 '15 at 09:47

1 Answers1

0

If you schedule an Intent with AlarmManager the application will be started when the intent is fired even if app has been closed.

Add the below in your activity:

AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, alarmIntent, 0);
        manager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent); //set 10 sec

Create a class AlarmReceiver

    public class AlarmReceiver extends BroadcastReceiver {
        PowerManager.WakeLock wl;

  @Override
        public void onReceive(Context context, Intent intent) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "backgroundwakelock");
            wl.acquire();

    //put your ui update code here
            wl.release();
        }
    }

Add below in Manifest file

<receiver android:name=".AlarmReceiver"/>
Psypher
  • 10,717
  • 12
  • 59
  • 83
  • I have timer task inside of onreceive method which is scheduled to run after 10 seconds. When I close the app at 5th second, the scheduled timer task also cancelled, then it is not firing up after 10 second. But during next iteration of alarm receiver is working as fine. – M Vignesh Jan 22 '15 at 07:33
  • I have acquired the wake lock at the time of setting the Alarm Manager. And releasing it at the time when the alarm manager cancelled. – M Vignesh Jan 22 '15 at 07:37
  • Why do you need to run Timertask in alarmreceiver? Set your Alarmreceiver to run at 10th second to update your UI without any timertask – Psypher Jan 22 '15 at 14:02
  • I am Working on Location aware Application. Already I have alarm Receiver to run on every 1 min to get the location information. Within that Alarm Receiver first 10 seconds I have to get the location from Network Provider. Next 10 second from GPS provider. Here is bad practice to set different Alarm Receiver inside an alarm receiver. And also it will consume more power. – M Vignesh Jan 22 '15 at 14:19
  • Did you call the alarm manager using intent? Just post your code for calling alarm manager. Another option to consider is to call a service in alarm to execute your task, after completion the task close the service. – Psypher Jan 24 '15 at 08:42