James Patrick
James Patrick

Reputation: 263

alarmManager approach to update data every 10 mins

I tried using

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleWithFixedDelay(new Runnable(){

            public void run() {
                // update server  (method which has asyncTask)
            }

        }, 0, 600, TimeUnit.SECONDS);

And it stops working after mobile sleeps. I would like to do the same using alarmManager but I dont know if I should use service,broadcast service or activity for the requirement above.

Please help me out.

Thanks in advance.

Upvotes: 0

Views: 942

Answers (1)

Robert Estivill
Robert Estivill

Reputation: 12477

Since you are going to be networking, i would recommend you to use a Service. If you don't need to keep state in between executions, or don't want to deal with threads manually, use an IntentService

This is the code i'm using to set up the a polling service:

    Intent intent = new Intent( context, YourService.class );
    PendingIntent pendingIntent = PendingIntent.getService( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );

    AlarmManager alarmManager = (AlarmManager) context.getSystemService( ALARM_SERVICE );
    alarmManager.setRepeating( AlarmManager.ELAPSED_REALTIME, POLLING_INTERVAL, POLLING_START_DELAY, pendingIntent );

Upvotes: 1

Related Questions