WindowManager无法始终绘制在顶部

时间:2016-03-10 13:31:41

标签: android popup android-service android-windowmanager android-broadcastreceiver

在我的应用中,我使用BroadcastReceiver来接听来电。因此,当有人打电话并且如果我将电话号码存储在我的应用程序的数据库中时,我会显示一个带有呼叫者姓名的窗口。

问题是,在某些设备中,如果在电话响铃之前设备的屏幕关闭,则不会显示此窗口。(如果设备的屏幕亮起,则当电话响铃时,会显示窗口) 。 我也推迟了窗口的绘制3秒钟,但这似乎不起作用。

要显示窗口,我使用以下代码,其中mViewRelativeLayoutTextView。此代码以Service

运行
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            dpToPx(72),
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

mParams.gravity = Gravity.TOP;
WindowManager mWindowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mView, mParams);

2 个答案:

答案 0 :(得分:0)

经过几次尝试失败后,我找到了它。

我必须将TYPE_PHONE替换为TYPE_SYSTEM_OVERLAY

答案 1 :(得分:0)

尝试一下,这肯定会起作用

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Layout_Flag = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Layout_Flag = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    }
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        Layout_Flag = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    }
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            Layout_Flag,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.CENTER;
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    try {
        mWindowManager.addView(windowView, params);