简体   繁体   English

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

[英]pop up wiindow with notification even apllication is not running

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

and i am getting this kisd of error like:我得到了这样的错误:

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

I have Added all permissions:我已添加所有权限:

 <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请解决这个问题我非常感谢您的回答并提前感谢您

You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert您需要有 ACTION_MANAGE_OVERLAY_PERMISSION 权限才能打开/显示警报

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

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

set alert type of "TYPE_APPLICATION_OVERLAY".设置警报类型为“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);
            }

change your TYPE_PHONE with TYPE_SYSTEM_ALERT使用TYPE_SYSTEM_ALERT更改您的TYPE_PHONE

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM