Android– 不活动活动,无论顶级应用程序

Android– 不活动活动,无论顶级应用程序,第1张

概述我需要找出最后一次用户交互的时间,无论哪个应用程序位于顶部.我不关心事件发生在哪里或什么,我只是需要知道它是什么时候.或者,当发生这种情况时,我会收到一个事件.我尝试过多种方法:>使用窗口创建服务并添加触摸侦听器.这吃了触摸事件并没有传递下来>寻找一个shell命令.geteve

我需要找出最后一次用户交互的时间,无论哪个应用程序位于顶部.我不关心事件发生在哪里或什么,我只是需要知道它是什么时候.或者,当发生这种情况时,我会收到一个事件.

我尝试过多种方法:

>使用窗口创建服务并添加触摸侦听器.这吃了触摸事件并没有传递下来
>寻找一个shell命令. getevent工作(每次收到触摸都会出现新线)但是你需要root,因此它不适合我.
>寻找“直到锁定的时间”,但没有提出任何结果.

另请注意:此处不存在安全问题,因为我不需要任何识别信息,例如触摸位置.只是一种印章(或现场活动).

我愿意使用反射来弄清楚它.

@ user2558882有一个非常好的解决方案.截至目前,这是我遇到过的最佳方法.

虽然这很好,但仍然需要用户在辅助功能控件中手动启用我们的应用程序.我们拥有数千台设备的客户,我们有办法自动更新和更改设置.我们尝试将手动配置保持在最低限度,但有些事情仍需要用户输入,例如启用设备管理模式.所以这个解决方案是可以接受的,但我仍然对一种不需要任何用户输入启用的方式开放.

我最终实现了@ user2558882的想法来使用辅助功能服务.虽然欢迎其他想法.

解决方法:

这只是一个想法,可能无法完全转移.

以下是AccessibilityService可以执行的 *** 作:

An accessibility service runs in the background and receives callbacks
by the system when AccessibilityEvents are fired. Such events denote
some state Transition in the user interface, for example, the focus
has changed, a button has been clicked, etc.

您将在onAccessibilityEvent(AccessibilityEvent)中被告知事件:

@OverrIDepublic voID onAccessibilityEvent(AccessibilityEvent event) {    // Some event    timeSinceLastInteraction = System.currentTimeMillis();}

您可以定期记录更新:

Log.i("LOG_TIME_ELAPSED", "Last user interaction was " +              ((System.currentTimeMillis() - timeSinceLastInteraction) / 1000) +                       " seconds ago.");

您可以通过两种方式配置AccessibilityService:

>在代码中,在onServiceConnected()中. (API 4起)
>在xml中,使用服务中的元数据标记. (API 14起)

在您的应用程序的情况下,您可以将AccessibilityServiceInfo.eventTypes设置为:

accessibilitySeviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;

但是,TYPES_ALL_MASK将包括以下通知:AccessibilityEvent.TYPE_ANNOUNCEMENT,AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED等,我猜你不在乎拦截.因此,您需要选择AccessibilityEvent.TYPE_X的子集.

您应该注意的另一件事是通知超时:

The timeout after the most recent event of a given type before an
AccessibilityService is notifIEd.

The event notification timeout is useful to avoID propagating events
to the clIEnt too frequently since this is accomplished via an
expensive interprocess call. One can think of the timeout as a
criteria to determine when event generation has settled down.

所以,要对超时值慷慨.

如果您决定使用AccessibilityService选项,则会发现此页面非常有用:Developing an Accessibility Service.

从您的评论到Chloe的答案,似乎设备在您的控制之下:意味着,在某种程度上,您不必依赖用户来启用该服务:

The lifecycle of an accessibility service is managed exclusively by
the system and follows the established service life cycle.
Additionally, starting or stopPing an accessibility service is
triggered exclusively by an explicit user action through enabling or
disabling it in the device settings.

您可以在部署时启用AccessibilityService,也可以使用类似AppLock的应用程序限制对“设置”菜单的访问.

另一个选择是检查您的AccessibilityService是否随时启用:

AccessibilityManager am = (AccessibilityManager)                               getSystemService(ACCESSIBIliTY_SERVICE);List<AccessibilityServiceInfo> listofServices =                              am.getEnabledAccessibilityServiceList(                               AccessibilityServiceInfo.FeedBACK_ALL_MASK);for (AccessibilityServiceInfo asi : listofServices) {    // Check if your AccessibilityService is running}

如果一个好奇/臭名昭着的用户已禁用AccessibilityService,您可以通过显示带有文本的全屏视图来锁定设备:设备已被锁定.联系销售代表以解锁此设备.

总结

以上是内存溢出为你收集整理的Android – 不活动/活动,无论顶级应用程序全部内容,希望文章能够帮你解决Android – 不活动/活动,无论顶级应用程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1096118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存