4

Is possible to know if user is currently interacting with a phone (touching the screen), on ANY activity (including launcher) on android device?

I would like to check in my app if any interaction was made in the last few seconds.

rootpanthera
  • 2,731
  • 10
  • 33
  • 65

1 Answers1

0

To detect a touch anywhere on your app you can simply add a onTouchListener to the main, outermost layout of you app.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/myLayout">
 <!- Your other view->
</Relativelayout>

If suppose this is your Xml file then in the activity java file you can do:

 RelativeLayout l = (RelativeLayout) findViewById(R.id.mylayout);
 l.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.d("screen","touched");
      //do stuff.

    }

 });

And if you want not just your app but anywhere and anytime to detect the touch no mater what app user is using. Then for that you have to create a service which will detect and keep recording all the touches. For more help check out this link

Detecting user activity in android

Community
  • 1
  • 1
Ashwani Kumar
  • 1,402
  • 1
  • 19
  • 26