Error calling dialog from Service Android

2019-08-26 19:44发布

问题:

I get the following android exception when I try to open a dialog.

09-20 09:27:46.119: W/System.err(558): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-20 09:27:46.139: W/System.err(558):  at android.view.ViewRoot.setView(ViewRoot.java:440)
09-20 09:27:46.139: W/System.err(558):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
09-20 09:27:46.139: W/System.err(558):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
09-20 09:27:46.139: W/System.err(558):  at android.app.Dialog.show(Dialog.java:269)
09-20 09:27:46.139: W/System.err(558):  at android.app.AlertDialog$Builder.show(AlertDialog.java:907)

I'm calling dialog from a Service Android, and I tried the following code:

handler.post(new Runnable() {
    public void run() {                               
        try{
            new AlertDialog.Builder(getApplicationContext()).setTitle("Alert!").setMessage("SIMPLE MESSAGE!").setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { 

                }
            }).show();
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }
});

回答1:

You can't open a dialog from a service. Dialog is a UI component and it needs to be associated with a UI element (Activity). What you can do is to start an activity from your service that "looks like" a dialog. You can give the UI of an Activity a "DialogTheme" so that it looks like a standard Andrdoid dialog. Just search StackOverflow for "activity dialog theme".



回答2:

i guess the problem is in getApplicationContext() may be it returns null, pass the right context...



回答3:

Step 1: Create class MyReciever:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startService(serviceIntent);
        }

        Intent serviceIntent = new Intent(context, MyService.class);
        context.startService(serviceIntent);
    }
}

Step 2: Create class MyService

import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public void onCreate() {

    //your SERVICE code here... 

        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

Step 3: In MainActivity class start Service:

startService(new Intent(this, MyService.class));