Android的 - 不活动/活动,无论顶部的应用程序 [英] Android - Inactivity/Activity regardless of top app

查看:121
本文介绍了Android的 - 不活动/活动,无论顶部的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出当最后一个用户交互是,不管哪一个应用程序是在顶部。我不在乎在哪里或什么事件是我只需要知道什么时候了。或者,当它发生时,我收到一个事件。

I need to find out when the last user interaction was, regardless of which application is on top. I don't care about where or what the event was I just need to know when it was. Or alternatively, as it happens, I receive an event.

我已经试过许多内容:

  1. 在一个窗口创建服务,并增加了一个触摸监听器。这个吃了触摸事件,并没有把它传递下来
  2. 找了一个shell命令。 getevent 作品(新线有每一个接收到触摸的时间),但是你需要root,所以它不是我一个合适的解决方案。
  3. 找了时间,直到锁,但想出了什么。
  1. Create service with a window and added a touch listener. This ate the touch event and didn't pass it down
  2. Looked for a shell command. getevent works (new line comes in every time a touch is received) however you need root and so it is not an appropriate solution for me.
  3. Looked for "time until lock" but came up with nothing.

另外请注意:有没有这个,因为我不需要任何身份信息,如触摸位置没有安全问题。只是一种标记(或现场活动)。

Also note: There is no security concern with this as I don't need any identifying information such as touch location. Just a type stamp (or live event).

我接受使用反射来弄明白为好。

I'm open to using reflection to figure it out as well.

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

@user2558882 has a very good solution. As of now, that's the best approach I've come across.

尽管这是伟大的,但仍然需要用户手动开启我们的访问控制应用程序。我们的客户有数千台设备,我们有办法来自动更新和更改设置。我们努力保持手工配置到最低限度,但有些东西还是需要用户输入如启用设备管理模式。所以这个解决方案是可接受的但我仍然是开放的,它不需要任何用户输入,以使一个方式。

While that's great, it still requires the user to manually enable our application in the accessibility controls. We have customers with thousands of devices and we have a way to automatically update and change settings. We try and keep manual configuration to a minimum, but some things still require user input such as enabling Device Admin mode. So this solution is acceptable however I'm still open to a way that doesn't require any user input to enable.

我最终实现@ user2558882的主意,使用辅助服务。虽然其他的想法是值得欢迎的。

I ended up implementing @user2558882's idea to use an accessibility service. Though other ideas are welcome.

推荐答案

这只是一个想法,可能不完全转让。

This is just an idea, and may not be fully transferable.

下面就是一个AccessibilityService可以做的:

Here's what an AccessibilityService can do:

这是辅助服务在后台运行,并接收回调   通过该系统,当AccessibilityEvents被解雇。这样的事件表示   在用户接口的一些状态转移,例如,焦点   已改变,一个按钮被点击,等等。

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)

You will be informed of the event inside onAccessibilityEvent(AccessibilityEvent):

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    // Some event

    timeSinceLastInteraction = System.currentTimeMillis();

}

您可以定期日志的更新:

You could periodically log the updates:

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

有您可以在其中配置AccessibilityService两种方式:

There are two ways in which you can configure your AccessibilityService:

  1. 在code,里面onServiceConnected()。 (API 4起)

  1. In code, inside onServiceConnected(). (API 4 onwards)

在XML中,使用你的服务的的元数据标记。 (API 14日起)

In xml, using the meta-data tag in your service. (API 14 onwards)

在应用程序的情况下,你很可能设置 AccessibilityServiceInfo.eventTypes 来:

In your application's case, you could probably set AccessibilityServiceInfo.eventTypes to:

accessibilitySeviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;

不过,TYPES_ALL_MASK将包括诸如通知:我猜你不在乎拦截AccessibilityEvent.TYPE_ANNOUNCEMENT,AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED等。所以,你需要选择AccessibilityEvent.TYPE_X的一个子集。

But, TYPES_ALL_MASK will include notifications such as: AccessibilityEvent.TYPE_ANNOUNCEMENT, AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED etc. which I am guessing you do not care to intercept. So, you'll need to choose a subset of AccessibilityEvent.TYPE_X.

另一件事你应该小心的是通知超时:

Another thing you should be careful about is the notification timeout:

一个给定类型之前,最近一次事件发生后超时   AccessibilityService通知。

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选项:的开发辅助功能服务

You'll find this page very helpful in case you decide to go with the AccessibilityService option: Developing an Accessibility Service.

从您的意见Chloe的答案,似乎该设备是你的控制之下:意义,在一定程度上,你不必再依靠用户启用该服务:

From your comments to Chloe's answer, it seems that the device is under your control: meaning, to some extent, you don't have to rely on the user for enabling the service:

可访问服务的生命周期完全由管理   该系统和遵循建立服务的生命周期。   此外,启动或停止辅助功能服务   通过授权或者通过明确的用户动作触发独家   禁用它在设备设置

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

You can enable the AccessibilityService at time of deployment, and perhaps restrict access to Settings menu using an app like AppLock.

另一种方法是检查从时间的AccessibilityService是否启用时间:

Another option is to check whether your AccessibilityService is enabled from time to time:

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已被禁用由好奇/臭名昭著的用户,则可以通过显示全屏视图文本锁定设备:设备已被锁定。联系销售代表,以解开这个设备

In case the AccessibilityService has been disabled by a inquisitive/notorious user, you can lock the device by displaying a fullscreen view with text: Device has been locked. Contact a Sales Rep to unlock this device.

这篇关于Android的 - 不活动/活动,无论顶部的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆