检测时,应用程序空闲的机器人 [英] Detect when application is idle in Android
问题描述
我开发,将运行在Kiosk模式的应用程序。在本申请中,如果用户没有在5分钟内的应用进行任何操作,应用程序将显示的屏幕保护程序是应用程序的标识。
I am developing an application that will be running in Kiosk Mode. In this application, if the user didn't do anything in the application within 5 minutes, the application will show a screen saver that is the logo of the application.
我的问题是,我怎么能$ 5分钟内检测空闲C $ C吗?
推荐答案
我想你可以使用<一个href="http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)" rel="nofollow">http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)和<一href="http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent)" rel="nofollow">http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent)在你的应用程序设置一个时间戳每次一个userinteraction发生(简单地重写方法并返回false在年底这样的事件将被传递到底层的视图) - 那么你可以使用某种类型的计时器其检查的最后一个时间戳互动recurringly并触发屏幕保护程序,如果你的5分钟空闲时间都达到了。
I think you could use http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent) and http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent) in your App to set a timestamp everytime a userinteraction takes place (simply override the methods and return false at the end so that the events will be propagated to underlying views) - then you can use some kind of timer which checks for the last timestamp of interaction recurringly and trigger your screen saver if your 5 minutes IDLE time are reached.
因此,在一个活动,你只需重写这样前面提到的方法:
So in an Activity you simply override the before mentioned Methods like this:
@Override
public boolean dispatchTouchEvent (MotionEvent ev) {
timestamp = System.getCurrentTimeMilis();
return false; // return false to indicate that the event hasn't been handled yet
}
在dispatchKeyEvent和其他方法,你可以重写,以确定用户的活动应该非常相似。
The dispatchKeyEvent and the other methods which you can override to determine user-activity should work fairly similar.
如果你使用一个以上的活动,你可能要创建延伸活动的基类,并覆盖所有dispatchXXXEvent要处理,哪些是你比你的所有活动的基类使用。但我想你的实现细节可能会有点超出范围的实际问题:)
If you're using more than one Activity you may want to create a base class which extends Activity and Override all the dispatchXXXEvent you want to handle and which you than use as base class of all your Activities. But I guess the details of your implementation may be a little bit out of scope for the actual question :)
有关定时器的不同的可能性,你可以在这里找到有用的信息:调度重复任务的机器人
For the different possibilities of timers you may find useful info here: Scheduling recurring task in Android
这篇关于检测时,应用程序空闲的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!