1

I am trying to create an application with a widget. When the user places the widget on the desktop a listview should come up with a list of items. The user selects an item then the widget is created with the respective text related to that item. I thought I should do this by showing a dialog in the Service but it throws me

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

to the dialog_newitem.show(); line. For simplicity I am using now a simple alertdialog.

Is it the way to do this? I haven't found anyhing about this on the net.

public class UpdateWidgetService extends Service {
    private static final String LOG = "de.vogella.android.widget.example";
    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    String value;
    Dialog dialog_newitem;
    EditText et_newitem;


    @Override
    public void onStart(Intent intent, int startId) {
         Toast.makeText(this, "UpdateWidgetService", Toast.LENGTH_SHORT).show();

            dialog_newitem = new Dialog(this);  //I tried UpdateWidgetService.this, too
            dialog_newitem.setContentView(R.layout.dialog_productlists_grp_capitalized);
            dialog_newitem.setTitle("Select");
            dialog_newitem.setCancelable(true);

            et_newitem = (EditText) dialog_newitem.findViewById(R.id.et_item_name);

            Button btn_Save = (Button) dialog_newitem.findViewById(R.id.btn_save_pr);
            btn_Save.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    value = et_newitem.getText().toString();
                }
                });


            Button btn_Cancel = (Button) dialog_newitem.findViewById(R.id.btn_cancel_pr);
            btn_Cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog_newitem.dismiss();
                }

            });
            dialog_newitem.show(); //error


         Toast.makeText(this, "value: " + value, Toast.LENGTH_SHORT).show();


    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

I have used this alertdialog in some other part of the code, and there it is working fine. I think it has something to do with the service.

erdomester
  • 11,789
  • 32
  • 132
  • 234

3 Answers3

4

You can't show a dialog in the service.

if you really want to show a dialog.

try to start an Activity and set the Activity's Theme to Theme.Dialog.

There is a demo in The ApiDemo Project

Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
2

I know this thread is old, but thought it would be worth contributing anyway for future sufferers.

Although most will say its not recommended to launch dialogs directly from a service, the following workaround works for me. Use the ServiceDialogBuilder class below to build your AlertDialog. Unlike the AlertDialog.Builder, this will work with a Service context and show() can be called directly from a service without having to start a new activity.

Just be wary that this is a bit of a hack, so there may well be some unintended side effects from doing this.

Hope this helps

public class ServiceDialogBuilder extends AlertDialog.Builder {

public ServiceDialogBuilder(Context context) {
    super(context);}

@Override
public AlertDialog create() {
    AlertDialog dialog=super.create();
    //Change dialog window type from TYPE_CHANGED to TYPE_SYSTEM_ALERT
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    return dialog;
}

@Override
public AlertDialog show()   {
    return super.show();
}}
JoshR
  • 33
  • 5
  • Thanks, but it is easier to show system alert as in http://stackoverflow.com/questions/7918571/how-to-display-a-dialog-from-a-service/19269931#19269931 – CoolMind May 05 '16 at 13:51
0

Just make sure your dialog's window is set to SYSTEM_ALERT:

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
Lesh_M
  • 596
  • 8
  • 6