问题描述
我开发了一个应用程序来收集前台服务类下的触摸事件。该应用程序有一个主要活动来输入用户的信息(例如,姓名和年龄)。然后前台服务开始收集触摸事件,并启动通知通知用户该服务正在运行。
这是主要活动代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
public boolean onoptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.run_catch:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent = new Intent(this,SensorService.class);
this.startForegroundService(intent);
setStartPreferences(true);
isstart = true;
} else {
intent = new Intent(this,SensorService.class);
this.startService(intent);
setStartPreferences(true);
isstart = true;
}
break;
case R.id.stop_catch:
intent = new Intent(getApplicationContext(),SensorService.class);
stopService(intent);
setStartPreferences(false);
isstart = false;
break;
}
return super.onoptionsItemSelected(item);
}
问题是无法在服务类下采集Touch事件。我的意思是 event.getAction() 函数在应用程序内部或外部都无法使用以下选项收集触摸事件: MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP 和 MotionEvent.ACTION_OUTSIDE。但是,在应用程序内部或外部点击时,我可以使用函数 gettime_in_ms() 以毫秒为单位收集触摸时间。
这是服务类代码:
public class SensorService extends Service implements SensorEventListener,LocationListener,View.OnTouchListener{
//Touch
private WindowManager windowManager;
private LinearLayout touchLayout;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
try {
initTouch();
} catch (Exception e) {
e.printstacktrace();
}
notification();
}
public void initTouch() {
touchLayout = new LinearLayout(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height =WindowManager.LayoutParams.MATCH_PARENT;
touchLayout.setLayoutParams(lp);
touchLayout.setonTouchListener(SensorService.this);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O){
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
// set layout parameter of window manager
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
0,LAYOUT_FLAG,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager.addView(touchLayout,mParams);
}
@Override
public boolean onTouch(View view,MotionEvent motionEvent) {
Toast.makeText(this,"Touch Event occur"+ "\n"+ gettime_in_ms() + "\n",Toast.LENGTH_SHORT).show();
int action = motionEvent.getAction();
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d("DEBUG_TAG","Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d("DEBUG_TAG","Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d("DEBUG_TAG","Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d("DEBUG_TAG","Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d("DEBUG_TAG","Movement occurred outside bounds " +
"of current screen element");
return true;
default :
break;
}
return true;
}
此代码始终将(移动发生在当前屏幕元素的范围之外)作为输出。 当用户在应用内部和外部点击时(我的意思是与手机上的其他应用交互时),我需要以毫秒为单位获取向下、移动和向上操作的时间。
这在服务类下是可能的吗?如果是,谁能提供示例代码、有用的链接或信息?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)