6
Android 5.0에서 MotionEvent.ACTION_DOWN을 감지하는 백그라운드 서비스를 실행하고 있습니다. 벨로우즈 코드를 사용하여 터치 이벤트를 감지 할 수 있지만 다른 앱을 터치 할 수는 없습니다. 어떻게 해결할 수 있습니까? 더 나은 해결책이 있다면 저를주십시오. 고마워.서비스 Android에서 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" />
이전에 사용해 보았습니다. 하지만 위의 코드를 사용할 때 어떤 앱도 누를 수 없습니다. 너 해볼래? – Jame
나는이 포스트 후에이 포스트를 시도했다. 만약 당신이 다른 어플리케이션 컨트롤에 접근하고 싶다면 그 코드는 불가능하다. –
감사합니다. 나는 그것을 시험했다. 지연 돼서 죄송합니다. 코드가 작동합니다. 그러나, 그것은 내 화면에 버튼을 보여 주었다. 나는 그것을 보이지 않게하려고 노력했지만, 보이지 않게 만들 때 터치가 나타나지 않을 것입니다. – Jame