问题描述
我希望做一些事情(调用一个函数),如果某个特定的时间间隔,屏幕并没有被感动还是被一直无所作为。
我怎样才能在Android中实现这一目标?
I wish to do something(call a function), if for a particular time interval the screen wasn't touched or was remained inactive. How can I achieve this in Android?
任何建议。
推荐答案
您可以在 onResume()
方法发送延迟的消息,UI线程的处理程序p>
You can send a delayed message to the handler of UI thread in onResume()
method:
handler.sendEmptyMessageDelayed(SCREEN_INACTIVE_MSG, DELAY_MILIS);
然后,在onTouchEvent删除该邮件,然后重新插入:
Then, in onTouchEvent remove the message and insert it again:
handler.removeMessages(SCREEN_INACTIVE_MSG);
handler.sendEmptyMessageDelayed(SCREEN_INACTIVE_MSG, DELAY_MILIS);
在您的处理程序,覆盖的handleMessage()
的方法,来处理消息:
In your handler, override handleMessage()
method, to handle the message:
public void handleMessage (Message msg) {
switch(msg.what) {
case SCREEN_INACTIVE_MSG:
handleIdleScreenMetohd();
break;
}
}
此外,删除从处理程序的消息中的onPause()
。
这假定您的活动是整个时间(未暂停等)的活跃。处理闲置屏幕的应用程序之外似乎是不可能的。
This assumes that your activity is active for the whole time (not paused etc.). Handling screen inactivity outside of your app seems impossible.
这篇关于如果屏幕并没有被感动,或一直处于非活动状态特定时间间隔,然后做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!