我正在创建启动服务的android活动。即使用户正在使用其他应用程序,此服务也旨在接收触摸事件。 它的onCreate()方法如下。 public void onCreate(){
super.onCreate();
// create linear layout
touchLayout = new LinearLayout(this);
// set layout width 30 px and height is equal to full screen
LayoutParams lp = new LayoutParams(30, LayoutParams.MATCH_PARENT);
touchLayout.setLayoutParams(lp);
// set color if you want layout visible on screen
//touchLayout.setBackgroundColor(Color.CYAN);
// set on touch listener
touchLayout.setOnTouchListener(this);
// fetch window manager object
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
// set layout parameter of window manager
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
//30, // width of layout 30 px
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE , // this window won't ever get key input focus
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.LEFT | Gravity.TOP;
Log.i(TAG, "add View");
mWindowManager.addView(touchLayout, mParams);
}
上面我创建了一个跨越屏幕高度和宽度的窗口。我把它设置为听取触摸事件。但这样做会阻止其他应用程序接收触摸事件。所以我希望将我服务上收到的这些触摸事件发送到我的窗口所在的后台应用程序。
请帮助!
答案 0 :(得分:0)
的Sandip,
向窗口管理器添加叠加视图会自动将该视图置于该窗口视图层次结构的顶部,这意味着它将拦截触摸而不是其后面的视图。接触触摸事件背后的唯一方法是让用户触摸顶视图之外(这是不可能的,因为它跨越整个屏幕),或者顶视图标记自己"不可触摸"
因此,要么限制视图的大小,要么将标志WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE添加到params标志。
在任何一种情况下,除非用户当前在前景中有您的应用,否则视图外的任何触摸事件都将返回ACTION_OUTSIDE触摸事件,但触摸位置没有任何坐标。 (如果用户确实将您的应用放在前台,那么您将收到ACTION_OUTSIDE事件的坐标。)