java - 如何 Intent 到android上的另一个页面/从空闲时间弹出一条消息?

标签 java android eclipse

晕,我首先想知道我的 Android 应用程序的空闲时间。之后,如果是空闲时间模式,我会做一些事情。

我点击此链接。 Application idle time

我的程序运行正常,但是突然出现问题。我无法移动到其他页面(例如登录页面)或使用 Alertdialog 弹出消息,因为它在线程中。你有什么解决办法?

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

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}

这是我的 ControlApplication.java

public class ControlApplication extends Application {
private static final String TAG=ControlApplication.class.getName();
private Waiter waiter;
@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "Starting application"+this.toString());
    //setContentView(R.layout.activity_main);
    waiter=new Waiter(5*60*1000); //5 mins
    waiter.start();
    Toast.makeText(ControlApplication.this, "start", Toast.LENGTH_LONG).show();
}

public void touch()
{
    waiter.touch();
    Toast.makeText(ControlApplication.this, "touch", Toast.LENGTH_LONG).show();
}   }

这里是 Waiter.java

public class Waiter extends Thread implements Runnable{
private static final String TAG=Waiter.class.getName();
private long lastUsed;
private long period;
private boolean stop;
Context activity;

public Waiter(long period)
{
    this.period=period;
    stop=false;
}

@SuppressLint("ParserError")
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
            //Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
            stopCounter();
        }
    }
    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 stopCounter()
{
    stop=true;
}

public synchronized void setPeriod(long period)
{
    this.period=period;
}}

我尝试创建一个新类并调用一个 Intent 方法。它也失败了。尝试从该方法弹出消息也失败。

你们对于空闲时间还有其他解决办法吗?谢谢。

问候, 阿尔弗雷德·安卡萨

最佳答案

在您的 Activity Activity 中,而不是此线程中,执行以下操作:

public class Graph extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                while(idle = 0) {
                    idle = System.currentTimeMillis()-lastUsed;

                    if(idle != period) {
                        Intent goNextActivity = new Intent(com.package.theactivity);
                    else {
                        idle == 0;
                    }
                }
             }
           }

关于java - 如何 Intent 到android上的另一个页面/从空闲时间弹出一条消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11533613/

相关文章:

java - 将列添加到包含数据的 Hibernate 表

java - 更新java数据库中的记录值

android - OnTouchListener 和 OnClickListener 的区别

java - 如何让我的广告横幅出现在我的应用程序上而不带走整个应用程序?

VeriSign 证书上的 Eclipse 信任证书窗口

java - 解析测试报告异常

java - Android:JsonWriter 无法写入文件

java - Android 应用程序中 setText 的空对象引用

java - 无法执行dex

Eclipse:如何创建和加载自定义键绑定(bind)方案?