52

I have read really a lot of posts about this topic, however nothing works for me (or doesn't have the effect I wish).

I have a an application, that after logging in starts a background Service (implementation of the Service class). This service syncs itself with a Server and if a new order comes, it creates a notification.

So far, everything works great, I have the notification and the Toast message. However, I would like to have a dialog, that notifies the user about the new order.

As I understood, you can start an activity from within the service, which displays the dialog. This works, but the activity starts on top of the current activity stack and displays the dialog. I have an activity with no view attached and it correctly displays the dialog, however, on a black background.

What I want is to display the dialog on the current activity, causing the actual background(the running activity) to fade and display the dialog.

Is this somehow possible?

Lii
  • 11,553
  • 8
  • 64
  • 88
Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • Possible duplicate of [Alert dialog from Android service](http://stackoverflow.com/questions/3599563/alert-dialog-from-android-service) – araks Nov 06 '15 at 17:35

5 Answers5

119

We can show dialog from service only if it is a system alert dialog. So, set TYPE_SYSTEM_ALERT window layout parameter to Dialog as follows:

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

But, it needs SYSTEM_ALERT_WINDOW permission. So, don't forget to add this permissin in Manifest file.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Edit: Better option to show dialog is to start activity as one of the below ways.

  1. Start Activity with Dialog Theme (android:theme="@android:style/Theme.Dialog") -(or)
  2. Start Translucent Activity and show Dialog in it.

Note: You should add Intent.FLAG_ACTIVITY_NEW_TASK to intent

chakrapani
  • 1,371
  • 2
  • 9
  • 6
  • @chakrapani this worked like a charm, but please how do I dismiss it??? Please see my question [http://stackoverflow.com/questions/21913249/unable-to-dismiss-alertdialog](http://stackoverflow.com/questions/21913249/unable-to-dismiss-alertdialog) – Lisa Anne Feb 20 '14 at 16:17
  • you are adorable, this should be the accepted answer – Muhammed Refaat Sep 28 '14 at 13:49
  • 11
    This should NOT be the accepted answer. Alert dialogs are for ALERTS only! It will be on top of everything, even for example over phone calls and battery status change dialogs. Be aware what you are doing if you use the param system_alert. – JacksOnF1re Dec 17 '14 at 15:44
  • 28
    @JacksOnF1re this SHOULD be the accepted answer as it answers the question. Nothing else matters – deej Jan 19 '15 at 01:34
  • 14
    @deej Do you want bad programers? Because that's how you get bad programers. "Nothing else matters" - that is the attitude why StackOverflow gets more and more a copy&paste-code example site. Sad, but true. – JacksOnF1re Jan 19 '15 at 10:13
  • 7
    @JacksOnF1re can't agree. No one is restricting you from explaining why something is bad, but it really should be a side note. Answer the question then explain that it is a bad practice because this and that and blah-blah. Current answer provides a workaround though. And it is even better because no additional permissions are required. Even so, I believe it is better to first answer the question. – deej Jan 19 '15 at 23:33
  • 1
    @deej I don't want to provide an answer. But I was ablte to explain why this is bad practice and why it should not be the answer. And that's only worth a comment. – JacksOnF1re Jan 20 '15 at 10:13
  • 1
    The fact that it requires a permission that can't ordinarily be obtained should do a fairly good job of protecting against misuse. There are legitimate uses of this. – Chris Stratton Mar 19 '15 at 21:06
  • Yes ,but the dialogue button style also has changed! – zionpi Aug 05 '15 at 08:02
  • 2
    @JacksOnF1re sometimes you need to actually display an alert on top of everything... like a critical alert.. if the app you are developing is somekind of low memory monitor service and you really want an alert when memory is low whatever you are doing.. then it is fine imo – yeahman Mar 05 '16 at 07:30
  • 3
    Ok don't let us argue about this anymore. If you're aware what you're doing, than everything is fine. Maybe this side note would have been enough and my reaction was a bit to harsh. – JacksOnF1re Mar 07 '16 at 11:22
  • 1
    Let's get serious: this is the right answer :-) Thanx! – Yoraco Gonzales Nov 09 '16 at 21:32
  • 1
    `TYPE_SYSTEM_ALERT` This constant was deprecated in API level 26. for non-system apps. Use [TYPE_APPLICATION_OVERLAY](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#TYPE_APPLICATION_OVERLAY) instead. – Shady Boshra Jun 07 '18 at 12:43
  • Thanks for the responses. Everyday is learning and we get better. I updated my post. thanks. – chakrapani Sep 02 '20 at 18:19
47

I highly, highly, HIGHLY recommend that you DON'T do this (it goes against Android design and UI guidelines). Notifications are the preferred way to accomplish what you are doing (which it sounds as if you have already accomplished).

That being said, if you must do it, I would recommend just using a Dialog themed activity. That way you don't have to start up a separate dialog. Please see http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme for how to do this.

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • This is what I have done so far. Is it actually the way, how for example a Call Notification is displayed? Whatever activity you are, If someone calls you, you get a dialog if you want to accept the call or not. Is it also just an activity with a DialogTheme? (Sorry, I am to lazy to look that up in the sources :) – Filip Majernik Oct 27 '11 at 16:35
  • 2
    I am not sure how that is handled. You are more than welcome to ask another SO question, but if my answer solved your problem, please be sure to click the checkbox next to my solution to accept it. – Justin Breitfeller Oct 27 '11 at 16:48
  • But with is it technically possible to display AlertDialog from service on their own? – Rahul Sundar Apr 18 '13 at 19:26
  • 1
    A use case for this: I have a notification with an action on it. Usually when the user selects the action it is executed in the background in a service. However sometimes an alert dialog should be shown. – Mark Gjøl Jan 26 '15 at 15:24
  • 1
    Starting an activity due to a button press in a notification is fine. But, it is also not the same as showing a dialog from a service. – Justin Breitfeller Jan 26 '15 at 21:04
  • this actually does not answer the question. – Vladyslav Matviienko May 25 '17 at 05:37
  • The answer is: Don't do it. If you are going to do it, start an activity that is themed to look like a dialog. Don't try and shoe-horn the dialog class into a service as its doomed to fail. – Justin Breitfeller May 25 '17 at 20:34
  • Dialog in a new Activity causes onPause() to be called for the current Activity displayed, and if noHistory=true for that activity, that activity will be destroyed. This might not be what you want. But then again, it might. Just be aware of that behaviour. – JohnyTex Jan 04 '18 at 10:31
4

you can start by learning on how to create an activity that looks like a dialog (no title bar, transparent background, "floating" effect, etc.) and no, you can't just start a dialog without an activty

josephus
  • 8,284
  • 1
  • 37
  • 57
1

No, you can't hijack activity that is not "yours" and command it to show dialog. Your approach of starting your own activity is the classic one.

Ognyan
  • 13,452
  • 5
  • 64
  • 82
1

You cannot show a dialog. But you can go the alernative way by inflating your customized view so that you can show a dialog on the screen whenver certain conditions are met.

Sandeep
  • 667
  • 7
  • 25