2

I'm having an issue with this one. My app has some asynctasks that are execute by an alarmmanager (like an alarm clock). I want that when this task is running, if the user is using the mainactivity of the app, a indeterminate progress bar shows.

I saw this post : android asynctask sending callbacks to ui

That uses an interface. But as it's not my activity which execute the task, I can't register the listener in the asynctask.

So, I'm stuck. Any ideas?

Community
  • 1
  • 1
user1391967
  • 497
  • 1
  • 6
  • 13
  • so where do you invoke asynctask if not in activity? – Raghunandan Feb 02 '14 at 07:24
  • **"execute by an alarmmanager (like an alarm clock)."** - When you say "alarmmanager" do you actually mean an alarm created by the Android `AlarmManager` service or something else? – Squonk Feb 02 '14 at 07:33
  • Yes, i'm using the android alarmanager to run the asynctask every hour to reach for data in the web. I do invoke the alarmmanger from the activity the first time. But then, the user can quit the activity and come back days after. – user1391967 Feb 02 '14 at 07:35
  • To me more specific, the alarmmanager trigers a broadcast that i receive on a broadcastreceiverclass and then the asynctask is executed. – user1391967 Feb 02 '14 at 07:54

1 Answers1

1

Firstly I'd recommend using an IntentService instead of an AsyncTask.

Secondly, create an inner class in your main activity which extends BroadcastReceiver.

Register an instance of the inner BroadcastReceiver in onResume() of your Activity and unregister it in onPause().

Have your first BroadcastReceiver start the IntentService and have the IntentService send a broadcast which the inner BroadcastReceiver of your Activity will be listening for if the Activity is running - the inner BroadcastReceiver can then create the progress bar.

When the IntentService finishes what it needs to do get it to send another broadcast to indicate that the progress bar should be dismissed.

Squonk
  • 48,735
  • 19
  • 103
  • 135