弹出窗口,即使没有运行应用程序也会发出通知 [英] pop up wiindow with notification even apllication is not running

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

问题描述

这是我的代码,我可以及时收到通知,但是在弹出窗口时应用程序崩溃了

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

推荐答案

您需要具有ACTION_MANAGE_OVERLAY_PERMISSION权限才能打开/显示警报

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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆