Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch AlertDialog.Builder from Service

Tags:

android

I am trying to launch checkbox dialog from Service by the use of AlertDialog.Builder but I am getting the following error:

This error when I launch the dialog without builder.getWindow().setType():

05-28 10:48:42.816: E/AndroidRuntime(18510): FATAL EXCEPTION: main
05-28 10:48:42.816: E/AndroidRuntime(18510): Process: com.bustracker, PID: 18510
05-28 10:48:42.816: E/AndroidRuntime(18510): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.Dialog.show(Dialog.java:312)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.stop_popup(TrackingService.java:370)
05-28 10:48:42.816: E/AndroidRuntime(18510):    at com.bustracker.TrackingService.onAsyncTaskFinished(TrackingService.java:305)

I tried to luanch it with builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

but I got this error:

The method getWindow() is undefined for the type

private void stop_popup(final ArrayList<Integer> routeList) {


    int routeListSize = routeList.size();

    if (routeListSize > 0) {

        String[] charSequence = new String[routeList.size()];
        for (int i = 0; i < routeList.size(); i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Has this route arrived the stop? ");

        builder.setMultiChoiceItems(charSequence, null,
                new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {

                        if (isChecked) {

                            route_number = routeList.get(which);

                        }  
                    }
                });

        builder.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });
        builder.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

        builder.create();
        builder.show();
    }
}
like image 951
Mr Asker Avatar asked Dec 02 '22 16:12

Mr Asker


1 Answers

If you want to popup a dialog in Android Service, you have two ways:

  1. Use an Activity as Dialog

  2. Use AlertDialog.Builder, but you'll need to config dialog as System Alert by using dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

Here is the sample code:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test dialog"));
builder.setIcon(R.drawable.icon);
builder.setMessage("Content");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //Do something
        dialog.dismiss();
});
builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        dialog.dismiss();
    }
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();

Also, remember to add permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
like image 178
Kartheek Avatar answered Dec 28 '22 03:12

Kartheek