12

I want to display an alert dialog from inside an intent service.

   AlertDialog alertDialog = new AlertDialog.Builder(this).create();

This throws the following exception

   Unable to add window — token null is not for an application

I have tried IntentService.this and getApplicationContext() as well. Between i dont want to do it using an activity. I just want to show a simple alert dialog with a little text.

Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42

4 Answers4

24

Need Activity for display AlertDialog, because we can't display Dialog from any Service

Solution.

Create Activity as Dialog Theme and start that Activity from Service.

Just need to register you Activity in menifest.xml like as below

android:theme="@android:style/Theme.Dialog"

or

android:theme="@android:style/Theme.Translucent.NoTitleBar"

MyDialog.java

public class MyDialog extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("your title");
        alertDialog.setMessage("your message");
        alertDialog.setIcon(R.drawable.icon);

        alertDialog.show();
    }
}
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Didnt want to use activity but it seems like its the only way. Thanks for the help. – Sayed Jalil Hassan Mar 25 '14 at 07:22
  • 6
    Also add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); while before starting activity – Maverick Dec 11 '14 at 05:35
  • 1
    works like sharm using `android:theme="@android:style/Theme.Translucent.NoTitleBar"` , with `android:theme="@android:style/Theme.Dialog"` after closing dialog it reapear title in other popup dialog – Choletski Jul 22 '15 at 08:23
11

Only if you set your alertDialog type to TYPE_SYSTEM_ALERT it will be displayed from an intent service.

 AlertDialog alertDialog = new AlertDialog.Builder(this).create();

add these after your code:

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();

But, it have a cost:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Eliran Kuta
  • 4,148
  • 3
  • 24
  • 28
  • Didn't work for me in Lollipop. `alertDialog.show()` is called but nothing happens. Yes, I have set the permission - before I did that, it raised an exception, and now it does nothing. – Stephen Hosking Jan 24 '17 at 11:52
  • Found the problem! Because this is running in an IntentService the dialog is destroyed as soon as `onHandleIntent` ends, which is almost immediately after the `alertDialog.show`. It happens so fast you don't even see it flash on the screen. The solution is to either use a Service (rather than IntentService) or this long running IntentService: http://stackoverflow.com/questions/6841212/can-an-intentservice-run-indefinitely – Stephen Hosking Jan 25 '17 at 01:33
1

Please visit

https://github.com/selmantayyar/Custom-SMS-Popup

it will surly help you!!

or what you can do is register anActivity in menifest.xml as follows

android:theme="@android:style/Theme.Dialog"

or

android:theme="@android:style/Theme.Translucent.NoTitleBar"

and work around it

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
-2

The problem is because of Context. You can't use this as Context in Intent Service. So need to pass a Context variable of your Intent Service to your Alert Dialog. Like,

AlertDialog alertDialog = new AlertDialog.Builder(context).create();