views:

66

answers:

3

I would like to implement a service in Android that basically should monitor the time elapsed since the user didn't touched the screen. For example the user opens Internet or Adobe Reader, starts reading and don't interact anymore withe the touchscreen. I want that my running service know the time since the user didn't touched the screen. How can I do this ? I'm thinking at two approaches: 1. The better one : After the time is elapsed (the service is set that after 2 or 5 or 10 minutes to check if the screen was or not touched) the service query some system object for this information. 2. The not so better one (I think this could have an impact on performance) At every touch of the screen the service to be notified.

Please help. Thanks.

+2  A: 

How can I do this ?

You don't.

After the time is elapsed (the service is set that after 2 or 5 or 10 minutes to check if the screen was or not touched) the service query some system object for this information.

There is no "system object" that will tell you when the user last touched the screen.

The not so better one (I think this could have an impact on performance) At every touch of the screen the service to be notified.

There is no way for a service to be notified about the user touching the screen.

CommonsWare
Ok, not surely convinced of this (the OS is doing this somehow, if you keep touching the screen then the screen will never go off so somehow the OS knows that the user is working on the phone...), then it has to be another method to find if the user interact with the phone or not...
Alex
@Alex: You are welcome to customize the Android firmware and have it do whatever you want. If, however, you are trying to create an Android SDK application, there is no means for a service to know what is going on with the UI, particularly the UI of other applications. There are many broadcast Intents that you can monitor, some of which (`ACTION_SCREEN_OFF` and `ACTION_USER_PRESENT`) are related to the data you seek. However, you failed to explain what you are trying to achieve, so we have limited ability to give you advice on whatever it is you are truly trying do to here.
CommonsWare
+1  A: 

You can't do this, because it requires access to data that an app could use to scrape data from other parts of the system that it otherwise wouldn't have access to. It's a security issue; the API doesn't provide a means to do it to avoid nefarious uses. The user would have to be using a modified ROM.

In any case, event listeners are bound to View objects; your own application's View objects are the only things you can watch for touch events on. Also, while we're at it--Services aren't meant for UI interaction (though apps can send signals and data to them while they're running).

Michael Louis Thaler
Ok, I begin to believe that this is not possible... :)
Alex
Ok, I begin to believe that this is not possible... :) Then I have another idea, that after the certain time elapsed, the service will pop up an alertdialog like "Hey, are you still there?" and if it doesn't get an answer then it could assume that the user is not there :)
Alex
@Alex: Doable, just potentially irritating. :) See CommonsWare's comment above though--the broadcast Intents may be more what you're looking for, allowing you to respond to the user apparently being present.
Michael Louis Thaler
A: 

Ok, maybe I wasn't to specific about what I want to do. The service acquire a FULL_WAKE_LOCK that will prevent the screen go off until the lock is released. I want that after certain amount of inactivity time the lock to be released automatically. Hope this clarify a bit. Thanks.

Alex