如何在android上调用另一个页面/从空闲时间弹出一条消息?

晕,第一个我想知道我的android应用程序的空闲时间。 之后,如果是空闲时间模式,我会做点什么。

我按照这个链接。 应用空闲时间

我的程序工作正常,但突然出现了问题。 我无法移动到其他页面(例如登录页面)或使用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 - eg 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。 它也失败了。 试图从该方法弹出一条消息也失败了。

你们有空闲时间的其他解决方案吗? 谢谢。

此致,Alfred Angkasa

在您的活动活动中,而不是此线程,执行:

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

我只是通过谷歌搜索找到答案,并尝试5个小时..:D

我希望我的回答对你也有帮助。

首先,我将ControlApplication和Waiter与ControlActivity混合使用。 这意味着我不需要两个文件。 我的ControlActivity将扩展活动(如果处于空闲模式,它将用于我对其他页面的意图),并且我将实现runnable(它用于我运行线程)。

之后我有一个名为onUserInteraction()的方法,这个方法可以帮助我在用户触摸或点击某些内容时获得用户交互。 在onCreate中,我启动所有变量,包括lastUsed,period和stop。

我为什么要发起这个? 因为你需要知道你的应用程序处于空闲模式的时间有多少秒。 这是期间使用。 停止变量用于我每隔5秒进行一次迭代和搜索(你也可以每隔一秒检查一次是否空闲)我的应用程序是空闲还是没有。 我通过调用方法touch启动lastUsed。 我将ControlApplication中的touch方法复制到了ControlActivity中。 通过调用触摸方法,我可以知道我最后一次使用的时间。 在那之后我开始我的线程。

在我的run方法中,我设置idle = 0.并进行一些循环检查。 我每5秒检查一次,知道我的应用程序是否处于空闲模式。 idle = System.System.currentTimeMillis() – lastUsed – >我使用它来知道空闲是否已经使用if或者不使用if方法。 如果空闲时间大于句点,我的应用程序必须处于空闲模式。 之后我停止迭代并使用处理程序来管理它。

我设置handler.sendEmptyMessage(0),并创建Handler。 在处理程序我移动到另一页。 这是我的完整代码。

 public class MainActivity extends Activity implements Runnable { private static final String TAG= MainActivity.class.getName(); private long lastUsed; private int period; private boolean stop; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); period = 10000; stop=false; touch(); Thread currentThread = new Thread(this); currentThread.start(); Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onUserInteraction() { super.onUserInteraction(); touch(); Log.d(TAG, "User interaction to "+this.toString()); } public synchronized void touch() { lastUsed=System.currentTimeMillis(); Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_SHORT).show(); } public void moveIntent() { Intent intent = new Intent(this, AfterIdle.class); startActivity(intent); } public void validate(View view) { switch (view.getId()) { case R.id.button1 : Intent intent = new Intent(this, AfterIdle.class); startActivity(intent); break; } } @Override public void run() { // TODO Auto-generated method stub long idle; while (!stop) { idle=System.currentTimeMillis()-lastUsed; try { Thread.sleep(5000); //check every 5 seconds } catch (InterruptedException e) { Log.d(TAG, "Waiter interrupted!"); } if (idle > period) { idle = 0; stop = true; } } handler.sendEmptyMessage(0); } public Handler handler = new Handler() { @Override public void handleMessage(Message msg) { moveIntent(); } };} 

我希望这段代码可以帮助其他人,如果他们遇到我上次遇到的同样的问题。 如果答案错误,我希望有人能为我纠正答案。 谢谢。

此致,Alfred Angkasa