应用程序空闲时间应用程序、时间

由网友(柠檬树下稚嫩少女i)分享简介:在我的应用程序有三个活动的一个 - 乙 - ç - >答:我想检测应用程序的空闲时间,让15分钟后,它会弹出一个消息,不论活动。什么是实现这一目标的最佳方法。 In my application there are three activities A -> B -> C -> A. I want to detect...

在我的应用程序有三个活动的一个 - 乙 - ç - >答:我想检测应用程序的空闲时间,让15分钟后,它会弹出一个消息,不论活动。什么是实现这一目标的最佳方法。

In my application there are three activities A -> B -> C -> A. I want to detect application's idle time, so that after 15 mins it will pop up a message irrespective of activity. what is the best method to implement this.

推荐答案

我会做它用这种方式:

创建线程,将控制闲置活动 运行此线程应用环境 在每个用户交互刚刚刷新的空闲时间

存储全局线程将控制空闲时间类

Class for storing global Thread which will control idle time

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();
    }
}

类,这将是父母对你所有的活动

Class which will be parent for all of your activities

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());
    }

}

最后线程本身

And finally Thread itself

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;
    }

}
阅读全文

相关推荐

最新文章