Java 如何在android上打开另一个页面/从空闲时间弹出消息?
Halo,首先我想知道我的android应用程序的空闲时间。之后,如果是空闲时间模式,我会做一些事情 我关注这个链接。 我的程序运行正常,但突然出现了问题。我无法移动到其他页面(例如登录页面)或使用alertdialog弹出消息,因为它位于线程中。你有什么解决办法吗Java 如何在android上打开另一个页面/从空闲时间弹出消息?,java,android,eclipse,Java,Android,Eclipse,Halo,首先我想知道我的android应用程序的空闲时间。之后,如果是空闲时间模式,我会做一些事情 我关注这个链接。 我的程序运行正常,但突然出现了问题。我无法移动到其他页面(例如登录页面)或使用alertdialog弹出消息,因为它位于线程中。你有什么解决办法吗 public class ControlActivity extends Activity { private static final String TAG=ControlActivity.class.getName(); /
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();
} }
这是water.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;
}}
我试图创建一个新类,并调用一个方法来表示意图。它也失败了。尝试从该方法弹出消息也失败
你们还有其他解决空闲时间的办法吗?谢谢
问候,,
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和Wayer与ControlActivity混合使用。这意味着我不需要两个文件。我的ControlActivity将扩展该活动(如果处于空闲模式,我将使用它来意图到另一个页面),我将实现runnable(我将使用它来运行线程) 之后,我有了一个名为onUserInteraction()的方法,该方法可以帮助我在用户触摸或单击某个对象时获得用户交互。 在onCreate中,我启动所有变量,包括lastUsed、period和stop 我为什么要发起这样的活动?因为你需要知道多少秒才能知道你的应用程序是否处于空闲模式。那是一段时间的使用。停止变量用于我每5秒迭代和搜索一次(你也可以让它每秒钟检查一次空闲与否),我的应用程序是否空闲。我通过调用方法touch启动lastUsed。我将触摸方法从ControlApplication复制到我的ControlActivity中。通过调用touch方法,我可以知道我最后一次使用的时间。之后,我开始我的线程 在run方法中,我将idle设置为0。然后做一些循环检查。我每5秒钟检查一次,以确定我的应用程序是否处于空闲模式。 idle=System.System.currentTimeMillis()-lastUsed->我使用它来知道idle是否已经与时段匹配,或者是否使用if方法。 如果空闲时间大于周期,则我的应用程序必须处于空闲模式。之后,我停止迭代,并使用处理程序来管理它 我设置了handler.sendEmptyMessage(0),并创建了handler。在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();
}
};}
我希望这段代码能帮助其他人,如果他们有我上次遇到的问题。如果我的答案是错的,我希望有人能帮我改正。
谢谢
问候,,
阿尔弗雷德·昂卡萨