1

I am trying to use a service to help me do counting down timer, when time is up, my main activity will show an alert dialog and when the user clicks on OK on the alert dialog, the activity will do some updates.

However, when the app is running in background, although my service is till running, it cannot pop up the alert dialog. Then, I cannot get the right updates. Is it possible to pop up the alert dialog when the app is running in background?

2 Answers2

0

I recommend using a notification to display an update to the user while your app is not in foreground. I think there are other ways to do it (like the FB notification that shows up everywhere), like system level alerts, but generally notifications are more suitable.

Eric S.
  • 1,502
  • 13
  • 31
  • Thanks for your answer. But I am curious that is there any method that I can use the alert dialog? – user3579602 Mar 06 '15 at 19:06
  • I dont think Android provides a way to do it legit, but you might be able to hack around it. [Here](http://stackoverflow.com/questions/18240413/how-to-prompt-to-user-an-alert-dialog-but-not-when-my-app-is-forground?rq=1) is an example. I think there are other ways too - like Facebooks notification bubble. For the record, I don't think it's a good idea to show something too intrusive from a background activity, but that's a question for ux.stackexchange – Eric S. Mar 06 '15 at 19:13
  • Thanks. One more thing, if I use notification to alert user your time is up, then where should I put it? In the activity or in the service? And can I differentiate app is foreground or background? Since I want to still use alert when it is foreground but use notification when it runs background. – user3579602 Mar 06 '15 at 20:31
0

As written here, the following code can be used to launch an activity from a service.

Intent dialogIntent = new Intent(getBaseContext(), myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

Now, that activity could very well be a dialog - a custom one at that.

I would very much be against this approach myself though. A service is supposed to be a background operation. Please make do with a notification if possible.

Community
  • 1
  • 1
Shashwat Black
  • 992
  • 5
  • 13
  • 24