【发布时间】:2017-03-06 01:25:29
【问题描述】:
我正在运行后台服务来检测 Android 5.0 中的 MotionEvent.ACTION_DOWN。我使用了下面的代码,它可以检测到我的触摸事件,但我无法触摸其他应用程序。我该如何解决?如果你有更好的解决方案,请给我。谢谢大家。
public class TouchServiceListener extends Service implements OnTouchListener {
private WindowManager mWindowManager;
// linear layout will use to detect touch event
private LinearLayout touchLayout;
@Override
public void onCreate() {
super.onCreate();
touchLayout = new LinearLayout(this);
// set layout width 30 px and height is equal to full screen
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 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(
WindowManager.LayoutParams.MATCH_PARENT, // width of layout 30 px
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);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_UP|motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Log.d(TAG, "Touch me");
}
return true;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
清单
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
【问题讨论】:
标签: android android-widget android-service