Unable to add window — token null is not for an application from Service

谁都会走 提交于 2019-12-13 14:10:58

问题


I have read many questions like this but none of them seemed to address my problem.

The problem lies with:

 AlertScreen ad = new AlertScreen(SensorListenerService.this);

in my Service class:

public class SensorListener extends Service implements SensorEventListener {

public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(Process.myPid(), new Notification());

    AlertScreen ad = new AlertScreen(SensorListener.this); //problem
    ad.show();

    return START_STICKY;
}
...

It calls my AlertScreen class:

public class AlertScreen extends AlertDialog {

public AlertScreen(Context context) {
    super(context);
}
...

What LogCat has to say:

Could anyone shed some light on the problem?


回答1:


You cannot show Dialogs from a Service context.

I suggest you to open an Activity that actually shows the dialog, or take a look at this answer to learn how to show system alerts.

https://stackoverflow.com/a/19269931/1725088



来源:https://stackoverflow.com/questions/23516689/unable-to-add-window-token-null-is-not-for-an-application-from-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!