当屏幕被触摸时如何在服务中获取事件?

发布于 2024-09-27 06:48:14 字数 275 浏览 0 评论 0原文

我想在 Android 中实现一个服务,它基本上应该监视自用户没有触摸屏幕以来所经过的时间。例如,用户打开 Internet 或 Adob​​e Reader,开始阅读并且不再与触摸屏交互。 我希望我正在运行的服务知道自用户没有触摸屏幕以来的时间。 我该怎么做? 我正在考虑两种方法: 1. 更好的一个:时间过去后(服务设置为在 2 分钟或 5 分钟或 10 分钟后检查屏幕是否被触摸),服务会查询某个系统对象以获取此信息。 2. 不太好的一个(我认为这可能会对性能产生影响)每次触摸屏幕时都会通知服务。

请帮忙。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-10-04 06:48:14

我该怎么做?

你不知道。

时间过去后(服务设置为在 2 分钟或 5 分钟或 10 分钟后检查屏幕是否被触摸),服务会查询某些系统对象以获取此信息。

没有“系统对象”可以告诉您用户上次触摸屏幕的时间。

不太好的一个(我认为这可能会对性能产生影响)每次触摸屏幕时都会通知服务。

服务无法收到有关用户触摸屏幕的通知。

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.

踏雪无痕 2024-10-04 06:48:14

您不能这样做,因为它需要访问数据,应用程序可以使用这些数据从系统的其他部分抓取数据,否则应用程序将无法访问这些数据。这是一个安全问题; API 没有提供避免恶意使用的方法。用户必须使用修改过的 ROM。

无论如何,事件监听器绑定到View对象;您自己的应用程序的 View 对象是您唯一可以监视触摸事件的对象。另外,当我们这样做时--服务并不用于 UI 交互< /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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文