写超时事件的android [英] writing a time out event for android

查看:159
本文介绍了写超时事件的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题我想不通。 我的计划,我基本上要执行一些code。如果用户没有5分钟,完成与应用程序任何东西(比如注销)。

i have a small issue i can't figure out. for my program, i basically want to execute some code if the user hasn't done anything with the application for 5 minutes (say log out).

我怎么能去这样做?我失去在检测到用户什么也没有做,然后重新计数,一旦用户触摸平板电脑什么的。有人可以给我一些指点?

how can i go about doing this? i'm lost on detecting that the user has done nothing, and then reset the count once the user has touched the tablet or something. can somebody give me some pointers?

在此先感谢!

推荐答案

所以,如果您有在您的应用程序单独的活动,那么你创建一个定时器和TimerTask来实现这一目标。并能追踪触摸和按键events.So在你的活动,你可以做这样的事情

So if you are having single Activity in your app then you create a Timer and TimerTask to achieve this. And can track touch and key events.So in your activity you can do something like this.

Timer longTimer;
synchronized void setupLongTimeout(long timeout) {
  if(longTimer != null) {
    longTimer.cancel();
    longTimer = null;
  }
  if(longTimer == null) {
    Timer longTimer = new Timer();
    longTimer.schedule(new TimerTask() {
      public void run() {
        longTimer.cancel();
        longTimer = null;
        //do your stuff, i.e. finishing activity etc.
      }
    }, 300000 /*delay in milliseconds i.e. 5 min = 300000 ms or use timeout argument*/);
  }
}
@override
public boolean onTouchEvent(MotionEvent me) {
  setupLongTimeout(300000);
  return super.onTouchEvent(me);
}
@override
public boolean onKeyUp(int keyCode, KeyEvent ke) {
  setupLongTimeout(300000);
  return super.onKeyUp(keyCode, ke);
}

如果您正在处理任意键/触摸事件的任何意见,那么你需要返回false,以便事件涉及到的活动。

If you are handling any key/touch events for any of the views then you need to return false so that event comes to the activity.

这篇关于写超时事件的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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