怎样才可以有屏幕的空闲监听? [英] How can i have screen idle listener?

查看:262
本文介绍了怎样才可以有屏幕的空闲监听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用闲置侦听用户正在使用的应用程序或空闲时,该活动还活着听。 我需要做一些事情,当用户不使用应用程序超过10秒。 我怎样才能使人们有可能?

I need to use idle listener to listen the user is using the application or idle when the activity is alive. I need to do something when the user is not using the application more than ten seconds. How can i make it possible?

推荐答案

下面是你的想法如何实现这一任务:

Here is the idea how you can achieve this task:

首先,你需要一个Runnable(),当你的超时时间(如10秒)时,将运行。下面是可运行():

Firstly you need a Runnable(), which will be Run when your timeout(e.g. 10 sec) occurs. Below is the Runnable():

private Runnable DoOnTimeOut = new Runnable() 
{
    public void run() 
    {
        // Do something Here
    }
}

现在,在你的活动,你可以调用postDelayed为DoOnTimeOut:

Now, in your activity, you can call postDelayed for the DoOnTimeOut:

Handler hl_timeout = new Handler();

@Override
public void onCreate(Bundle b)
{
   hl_timeout.postDelayed(DoOnTimeOut, 10000); // The DoOnTimOut will be triggered after 10sec
}

现在,最重要的是,当你看到用户的交互,要取消呼叫DoOnTimeOut,然后重新设定的要求,持续10秒。这是你的活动的用户交互的覆盖方式:<​​/ P>

Now, most important part is that when you see user interaction, you want to cancel the call to DoOnTimeOut and then again set the call for next 10 sec. Here is the Override method of your Activity for User Interaction:

@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    //Remove any previous callback
    hl_timeout.removeCallbacks(DoOnTimeOut);
    hl_timeout.postDelayed(DoOnTimeOut, 10000);
}

我希望这将是对你有所帮助。

I hope it will be helpful for you.

这篇关于怎样才可以有屏幕的空闲监听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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