Get Exception when showing Alert Dialog from Service - Unable to add window — token null is not for an application

后端 未结 5 1750
遥遥无期
遥遥无期 2020-12-06 19:11

Get Exception when showing Alert Dialog from Service.

Following is the code in my Service class: I am trying to show an AlertDialog.

But I get the error: Una

5条回答
  •  余生分开走
    2020-12-06 19:40

    Instead of creating a new transparent activity, Use the SYSTEM_ALERT_WINDOW permission to do this

    public void onReceive(Context ctx, Intent intent) {
        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popup = inflater.inflate(R.layout.mypopup, null, false);
        CustomDialog dialog = new CustomDialog(ctx, popup );
    
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    
        dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation;
        dialog.show();
    }
    

提交回复
热议问题