应用空闲时间

时间:2010-11-02 05:43:28

标签: android

在我的申请中有三项活动A - > B - > C - >答:我想检测应用程序的空闲时间,这样在15分钟后它会弹出一条消息而不管活动。实现这个的最佳方法是什么。

3 个答案:

答案 0 :(得分:63)

我会这样做:

  1. 创建将控制空闲活动的线程
  2. 在应用程序环境中运行此线程
  3. 在每次用户交互时只刷新空闲时间
  4. 用于存储将控制空闲时间的全局线程的类

    public class ControlApplication extends Application
    {
        private static final String TAG=ControlApplication.class.getName();
        private Waiter waiter;  //Thread which controls idle time
    
        // only lazy initializations here!
        @Override
        public void onCreate()
        {
            super.onCreate();
            Log.d(TAG, "Starting application"+this.toString());
            waiter=new Waiter(15*60*1000); //15 mins
            waiter.start();
        }
    
        public void touch()
        {
            waiter.touch();
        }
    }
    

    将成为您所有活动的父级的类

    public class ControlActivity extends Activity
    {
        private static final String TAG=ControlActivity.class.getName();
    
        /**
         * Gets reference to global Application
         * @return must always be type of ControlApplication! See AndroidManifest.xml
         */
        public ControlApplication getApp()
        {
            return (ControlApplication )this.getApplication();
        }
    
        @Override
        public void onUserInteraction()
        {
            super.onUserInteraction();
            getApp().touch();
            Log.d(TAG, "User interaction to "+this.toString());
        }
    
    }
    

    最后是自己的线程

    public class Waiter extends Thread
    {
        private static final String TAG=Waiter.class.getName();
        private long lastUsed;
        private long period;
        private boolean stop;
    
        public Waiter(long period)
        {
            this.period=period;
            stop=false;
        }
    
        public void run()
        {
            long idle=0;
            this.touch();
            do
            {
                idle=System.currentTimeMillis()-lastUsed;
                Log.d(TAG, "Application is idle for "+idle +" ms");
                try
                {
                    Thread.sleep(5000); //check every 5 seconds
                }
                catch (InterruptedException e)
                {
                    Log.d(TAG, "Waiter interrupted!");
                }
                if(idle > period)
                {
                    idle=0;
                    //do something here - e.g. call popup or so
                }
            }
            while(!stop);
            Log.d(TAG, "Finishing Waiter thread");
        }
    
        public synchronized void touch()
        {
            lastUsed=System.currentTimeMillis();
        }
    
        public synchronized void forceInterrupt()
        {
            this.interrupt();
        }
    
        //soft stopping of thread
        public synchronized void stop()
        {
            stop=true;
        }
    
        public synchronized void setPeriod(long period)
        {
            this.period=period;
        }
    
    }
    

答案 1 :(得分:5)

所以我个人会使用AlarmService。您可以指定将启动显示对话框的活动的PendingIntent。在应该重新启动计时器的任何事件之后,您只需取消pendingIntent并重新注册它。

答案 2 :(得分:1)

我认为CountDownTimer是做到这一点的最佳方法。

    public class IdleCountDownTimer extends CountDownTimer {
        public IdleCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            //Time lapsed 
           Toast.makeText(getApplicationContext(),"The text you want to display",Toast.LENGTH_LONG)

        }

        @Override
        public void onTick(long millisUntilFinished) {
        }
    } 

现在声明全局对象

IdleCountDownTimer idleCountDownTimer;

现在初始化并开始倒数。

@Override
public void onCreate()
{
    super.onCreate();
    idleCountDownTimer = new IdleCountDownTimer(seconds * 60 * 1000, interval_in_seconds * 60 * 1000);
    idleCountDownTimer.start();
}

现在要取消倒数计时,呼叫取消并开始。

例如1:针对所有按键,触摸或轨迹球事件

@Override
public void onUserInteraction(){
    super.onUserInteraction();

    //Reset the count down
    idleCountDownTimer.cancel();             
    idleCountDownTimer.start();
}

例如2:仅适用于触摸事件

@Override
public boolean onTouchEvent(MotionEvent event) {
        //Reset the count down
        idleCountDownTimer.cancel();             
        idleCountDownTimer.start();

        // Let the event flow
        return false;
}