即使应用程序未运行,也会弹出带有通知的窗口

pop up wiindow with notification even apllication is not running(即使应用程序未运行,也会弹出带有通知的窗口)

本文介绍了即使应用程序未运行,也会弹出带有通知的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我会按时收到通知,但应用程序在弹出过程中出现 ctrash

here is my code and I am getting my notification on time but the application is being ctrash during pop up

 private void showCustomPopupMenu()
{
    WindowManager windowManager2 = (WindowManager) App.getAppContext().getSystemService(WINDOW_SERVICE);
    LayoutInflater layoutInflater=(LayoutInflater)App.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view=layoutInflater.inflate(R.layout.window_popup_medicine, null);

    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }

  WindowManager.LayoutParams  params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

        params.gravity=Gravity.CENTER|Gravity.CENTER;
        params.x=0;
        params.y=0;
    assert windowManager2 != null;
    windowManager2.addView(view, params);
    }

我得到了这样的错误:

Unable to add window android.view.ViewRootImpl$W@46b5050 -- permission denied for window type 2038

我已添加所有权限:

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

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

<uses-permission
    android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
    tools:ignore="ProtectedPermissions" />

请解决这个问题,非常感谢您的回答并提前感谢您

please resolve this I would really appreciate your answer and thank you in advance

推荐答案

您需要有 ACTION_MANAGE_OVERLAY_PERMISSION 权限才能打开/显示 Alert

You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert

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

  <uses-permission
        android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
        tools:ignore="ProtectedPermissions" />

设置警报类型为TYPE_APPLICATION_OVERLAY".

set alert type of "TYPE_APPLICATION_OVERLAY".

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            }else{
                alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }

将您的 TYPE_PHONE 更改为 TYPE_SYSTEM_ALERT

您还应该参考 这个 回答.如果您仍有疑问,请告诉我.

You should also refer this answer. If still you have doubt let me know.

现在当通知到达时,检查一下:

Now when notification arrived, check this:

  public void notificationArrived(String myMsg){

      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                final boolean overlayEnabled = Settings.canDrawOverlays(MyFirebaseMessagingService.this);
                Global.printLog("showTaskDetailPopup==", "overlayEnabled" + overlayEnabled);

                if (!overlayEnabled) return;
            }

  new Handler(Looper.getMainLooper()).post(new Runnable() {
            public void run() {

                final Dialog dialog = new Dialog(MyFirebaseMessagingService.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.window_popup_medicine);
                dialog.setCancelable(true);

                TextView tv_msg = dialog.findViewById(R.id.tv_msg);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                    } else {
                            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                    }
                    dialog.show();
                }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
     }

这篇关于即使应用程序未运行,也会弹出带有通知的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:即使应用程序未运行,也会弹出带有通知的窗口

基础教程推荐