Question

I would like to detect when was the last time the user interacted with the screen. I'm not interested in doing any malware/spyware stuff, just need to calculate how much time has elapsed since the last time the user tapped on the screen.

The goal is to achieve a functionality similar to that of a keyguard.

I've been doing some research and following some to Q&A on the site (such as Android Best Way to Detect and Handle User INACTIVITY, Android: Detect General Use by User among others) but when it comes to detect user interaction in android I haven't found what I need.

Thanks in advance.

Was it helpful?

Solution

After some more research and testing I've managed to rise with a solution.

What I did was to create a background service in which I get an instance of the WindowManager and add a zero sized view that get's notified of user touches outside of it... but as the view has zero size it can get called every time. For that we may check https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

The InactivityService.java class detects every time the user taps the screen and write it to the preferences so it can be displayed or employed later in other components of the app. It's valid to notice that we can broadcast the time, or pass it to our app object or whatever other solution suits or needs.

/**
 * Detects user taps on screen (user interaction).
 * 
 * @author Junior Buckeridge A.
 *
 */
public class InactivityService extends Service {

    protected static final String LOG_TAG = InactivityService.class.getSimpleName();
    private boolean isAdded = false;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if(!isAdded){
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    0, 0, 0, 0,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            View view = new View(this);
            view.setOnTouchListener(new OnTouchListener() {

                @SuppressLint("DefaultLocale")
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    GregorianCalendar calendar = new GregorianCalendar();
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(InactivityService.this);
                    String log = prefs.getString(LOG_KEY, "");
                    log = String.format("User interaction detected at: %02d:%02d:%02d \n%s", 
                                calendar.get(Calendar.HOUR_OF_DAY),
                                calendar.get(Calendar.MINUTE),
                                calendar.get(Calendar.SECOND),
                                log);
                    prefs.edit().putString(LOG_KEY, log).commit();

                    return false;
                }
            });
            wm.addView(view, params);
        }

        return START_STICKY;
    }
}

We should add the following permission to the manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Also valid to mention that this method, combined with getting the running tasks with ActivityManager is a very effective way to determine if the user is interacting with the handset.

OTHER TIPS

You could use an AccessibilityService, which would allow you to listen for click, scroll, and other interaction events. One limitation is that it doesn't report raw touch events unless they trigger actions.

I managed to improve the solution of Junior Buckeridge such that the SYSTEM_ALERT_WINDOW permission is not required anymore. The idea is the same: we add a view that receives all onpress events. But my solution just adds this view in the onCreate of the activity and uses the WindowManager.LayoutParams.TYPE_APPLICATION instead of WindowManager.LayoutParams.TYPE_SYSTEM_ALERT when creating the LayoutParams.

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            0, 0, 0, 0,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    View view = new View(this);
    view.setOnTouchListener(new View.OnTouchListener() {

        @SuppressLint("DefaultLocale")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("Test", "Hello World!");

            return false;
        }
    });
    wm.addView(view, params);

So I am using @Junior Buckeridge solution, but actually I got a crash, since we are not allowed to draw over the application window. (Bad token exception)

What really helped me:

windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
//here is all the science of params
final LayoutParams myParams = new LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        LayoutParams.TYPE_SYSTEM_ERROR,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
        PixelFormat.TRANSLUCENT
);

in your Activity:

 if(Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(Activity.this)) {
    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, 1234);


   }
}else{
    Intent intent = new Intent(Activity.this, Service.class);
    startService(intent);
}

and also add the:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

source: Android: Unable to add window. Permission denied for this window type (Anam Ansari's answer)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top