我有以下服务来显示系统中每个应用的叠加视图并获取触摸事件。由于TYPE_SYSTEM_ALERT,触摸无法进入背景,因此无用。我以为我可以获得一个MotionEvent并使用我刚刚检测到的相同的x,y将它发送到背景视图。
**问题:如何定义我将发送事件的背景视图?鉴于我可以随时检测前台应用程序是什么,我可以定义该窗口的视图以便我可以调度该事件吗?
public class OverlayService extends Service {
LinearLayout oView;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
oView = new LinearLayout(this);
oView.setBackgroundColor(0x88ff0000); // The translucent red color
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(oView, params);
oView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("Recycle")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_DOWN,
x,
y,
metaState
);
Toast.makeText(getBaseContext(), Integer.toString(x)+" "+Integer.toString(y), Toast.LENGTH_SHORT).show();
// Dispatch touch event to background
// ?
// ?
// Dispatch touch event to background
}
// Obtain MotionEvent object
return false;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
if(oView!=null){
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(oView);
}
}
}