Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a Dialog from a Service?

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?

like image 359
Filip Majernik Avatar asked Oct 27 '11 15:10

Filip Majernik


People also ask

Can we show dialog from service?

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.

Can I show dialog from service android?

From a Service, you can easily show a Material Design styled Dialog manipulating its Window type, attributes and LayoutParams.

How do I show custom dialog?

This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

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

like image 197
chakrapani Avatar answered Oct 22 '22 02:10

chakrapani


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.

like image 32
Justin Breitfeller Avatar answered Oct 22 '22 02:10

Justin Breitfeller