Get touch data under service class in android studio

僤鯓⒐⒋嵵緔 提交于 2021-01-29 08:59:10

问题


I developed an App to collect the touch events under the foreground service class. The app has main activity to input user's information (e.g., name and age). Then when "RUN" button on the menu items is selected by the user, a foreground service starts to collect touch events, and a notification is launched to inform the user that the service is running.

This is the main activity code

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);
}

Inside the Oncreate function of the service class, I initialized the touch function using the new "Linear layout" with the "WindowManger".

This is the service class code:

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() {  //Touch
    touchLayout = new LinearLayout(getApplicationContext());
    touchLayout.setOnTouchListener(this);
    windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE));
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    touchLayout.setLayoutParams(params);
    windowManager.addView(touchLayout, params);
     }


 @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    Toast.makeText(this, "Touch Event occur"+ "\n"+ motionEvent.getX() + "\n"+ motionEvent.getY(), Toast.LENGTH_SHORT).show();
  return true;
}

I also tried to call the initTouch() function from inside "onStartCommand()" but still not working. Can anyone help me to get touch events (e.g., X, Y coordinates of the touches) of the user when interacting with our app's linear layout (or other apps in the smartphone) while the app running under the service class?

来源:https://stackoverflow.com/questions/65299277/get-touch-data-under-service-class-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!