Tôi đang chạy dịch vụ nền để phát hiện MotionEvent.ACTION_DOWN trong Android 5.0. Tôi đã sử dụng mã dưới đây và nó có thể phát hiện sự kiện chạm của tôi nhưng tôi không thể chạm vào các ứng dụng khác. Làm thế nào tôi có thể sửa chữa nó? Nếu bạn có một giải pháp tốt hơn, xin vui lòng cho tôi. Cảm ơn tất cả.Cách phát hiện MotionEvent.ACTION_DOWN trong dịch vụ Android
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;
}
}
Manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Tôi đã thử trước đây. Nhưng nó không thể nhấn bất kỳ ứng dụng nào khi tôi sử dụng mã trên. Bạn có thể thử? – Jame
Tôi đã thử mã này sau khi đăng bài ở đây.Nếu bạn muốn truy cập một điều khiển ứng dụng khác thì không thể. –
Cảm ơn bạn. Tôi đã thử nghiệm nó. Xin lỗi về sự chậm trễ. Mã của bạn đã hoạt động. Tuy nhiên, nó cho thấy một nút trên màn hình của tôi. Tôi đã cố gắng để làm cho nó vô hình, nhưng các liên lạc sẽ không hiển thị khi tôi làm cho nó vô hình. – Jame