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 min开发者_开发技巧utes 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.
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.
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).
精彩评论