我怎么知道app空闲时间?

时间:2012-06-27 10:34:19

标签: android idle-timer

我有一个Android应用程序它显示web视图。我想知道应用程序空闲时间因为如果会话过期我想去我的Android登录活动我该怎么办呢。请为我提供任何建议。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以在暂停方法内启动计时器,一旦恢复活动,您可以通过计时器获得持续时间。代码参考:

// create a instance for chronometer
Chronometer duration = new Chronometer(this);

// starts chronometer inside pause override method
duration.setBase(0);
duration.start();

// you also need to add onTick listener.
duration.setOnChronometerTickListener(new OnChronometerTickListener(){
        @Override
        public void onChronometerTick(Chronometer arg0) {
            NumberFormat formatter = new DecimalFormat("00");
            long countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
            String asText = formatter.format(countUp / 60) + ":" + formatter.format(countUp % 60);
        if (is_pause == false) {
            duration_time = asText;
        } 

// once done stop the chronometer inside stop override method and set a flag, check      this falg inside OnTickListener and get the duration asText.
duration.stop();
is_pause = false;