我正在开发一个Android应用程序,该程序可用作后台服务,旨在记录所有触摸事件的坐标。我尝试了几种解决方案和建议,但是它们并没有达到我的目标。我定义了一个GlobalTouchService扩展服务实现View.OntouchListener。当应用程序在前台工作时,它可以工作,但是当其他应用程序在前台工作时,我想检测触摸事件。当其他应用程序在前台工作时,此解决方案始终返回零作为触摸坐标。
这是我的Mainactivity:
package com.token.simpledatabaseproject;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.icu.text.AlphabeticIndex;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class GlobalTouchService extends Service implements View.OnTouchListener {
WindowManager.LayoutParams params;
private String TAG = this.getclass().getSimpleName();
private WindowManager mWindowManager;
private LinearLayout touchLayout;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@SuppressLint("ClickableViewaccessibility")
@Override
public void onCreate() {
super.onCreate();
touchLayout = new LinearLayout(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
touchLayout.setLayoutParams(lp);
touchLayout.setBackgroundColor(Color.CYAN);
touchLayout.setOnTouchListener(this);
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,WindowManager.LayoutParams.flaG_NOT_FOCUSABLE
| WindowManager.LayoutParams.flaG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.flaG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.flaG_LAYOUT_NO_LIMITS,PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
params.x = 0;
params.y = 100;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(touchLayout,params);
}
@Override
public void onDestroy() {
if (mWindowManager != null) {
if (touchLayout != null) mWindowManager.removeView(touchLayout);
}
super.onDestroy();
}
@Override
public boolean onTouch(View v,MotionEvent event) {
Log.d("Touchhhhhhhhhhhhhh: ",String.valueOf(event.getX()) + String.valueOf(event.getY()));
return true;
}
}
这是我的GlobalTouchService:
package com.token.simpledatabaseproject;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class GlobalTouchService extends Service implements View.OnTouchListener {
WindowManager.LayoutParams params;
private String TAG = this.getclass().getSimpleName();
private WindowManager mWindowManager;
private LinearLayout touchLayout;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@SuppressLint("ClickableViewaccessibility")
@Override
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(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);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
params.x = 0;
params.y = 100;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(touchLayout,params);
}
@Override
public void onDestroy() {
if (mWindowManager != null) {
if (touchLayout != null) mWindowManager.removeView(touchLayout);
}
super.onDestroy();
}
@Override
public boolean onTouch(View v,MotionEvent event) {
Log.d("Touchhhhhhhhhhhhhh: ",String.valueOf(event.getX()) + String.valueOf(event.getY()));
return true;
}
}