Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alert dialog from inside an intent service

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.

like image 990
Sayed Jalil Hassan Avatar asked Mar 25 '14 06:03

Sayed Jalil Hassan


1 Answers

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();
    }
}
like image 146
Niranj Patel Avatar answered Oct 03 '22 21:10

Niranj Patel