Android – start alertdialog.builder from service

I tried to launch the check box dialog box from the service by using alertdialog.builder, but I received the following error:

This error occurs when I start a 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 try to use builder. Getwindow(). SetType (WindowManager. Layoutparams. Type_system_alert);

But I received this error:

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();
    }
}

resolvent:

If you want to pop up a dialog box in the Android service, there are two methods:

>Use activity as dialog > use alertdialog. Builder, but you need to configure the dialog as a system alarm by using dialog. Getwindow(). SetType (WindowManager. Layoutparams. Type_system_alert);

The following is 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 permissions in androidmanifest. XML

<uses-permission android:name="android.permission.SYstem_ALERT_WINDOW" />

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>