简体   繁体   中英

How to get an event in a service when the screen is touched?

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM