FLAG_NOT_TOUCH_MODAL 不工作/Android

FLAG_NOT_TOUCH_MODAL not working / Android

我正在 Android 上开发应用程序。我的应用程序本质上是一个通话记录。我想要的是当设备收到来电时,我的应用程序在所有其他活动上方显示三个按钮。

我现在拥有的是 BroadcastReceiver,它接收 PHONE_STATE 意图,当检测到来电时,会启动 activity。启动的activity包含我要显示的按钮。

当OverlayActivity出现时,下面的所有Activity都停止接收触摸事件,即我只能触摸OverlayActivity。如何让他们收到事件?

现在在我的 BroadCastReceiver 中我开始 activity:

//PhonecallReceiver.java
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(myContext, OverlayActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("phone_number", number);
    context.startActivity(i);
}

实际情况 activity

//OverlayActivity.java:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

                WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.CENTER | Gravity.TOP;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = getLayoutInflater();
    mLayout = (LinearLayout) inflater
            .inflate(R.layout.overlay_layout, null);
    wm.addView(mLayout, params);
    }

@DJClayworh

感谢提示:)

我设法使用与考试相同的代码解决了我的问题,但我没有使用 Activity,而是使用了服务。您只需启动服务即可显示叠加层。这是一个例子:

public class OverlayService extends Service {

    private WindowManager windowManager;
    private View layout;

    @Override
    public void onCreate() {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            // very important, this sends toush events to underlying views
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
            PixelFormat.TRANSLUCENT);
        // Layout gravity
        params.gravity = Gravity.TOP | Gravity.CENTER;
        params.x = 0; params.y = 100;

        LayoutInflater li = (LayoutInflater) getApplicationContext()
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Your overlay layout
        layout = li.inflate(R.layout.overlay, null;

        // This displays overlay on the screen
        windowManager.addView(layout, params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Error will occur if this is missing.
        if (layout != null)
            windowManager.removeView(layout);
    }
}