[英]How to start activity after some time in Android?
I am developing and android application in that I want to start another application(my second application). 我正在开发android应用程序,因为我想启动另一个应用程序(第二个应用程序)。 I am doing the following code 我正在执行以下代码
Intent i= new Intent();
i.setComponent(new ComponentName("my second app package","my class name"));
startActivity(i);
It is working fine. 一切正常。
I want to hide the second application after 3 or 5 seconds for that I am following the below code 我想在3或5秒后隐藏第二个应用程序,因为我正在执行以下代码
Timer t=new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent("first app package","first app class name" );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 5000);
But this code is not working as I expected only for my second application. 但是此代码无法正常工作,仅对第二个应用程序有效。 But other applications are working fine. 但是其他应用程序运行良好。 Instead thread I also tried Handler, alaram manager also no sucess. 相反,线程我也尝试了Handler,alaram管理器也没有成功。 Please any one help me in this. 请任何人在这方面帮助我。
Is I want to do any code change in my second application or what is the problem with my code? 我是否要在第二个应用程序中进行任何代码更改,或者我的代码有什么问题?
Thanks 谢谢
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// your code to start second activity. Will wait for 3 seconds before calling this method
startActivity(new Intent(FirstActivityClass.this,SecondActivityClass.class));
}
}, 3000);
Use above code after onCreate of first activity 在第一个活动的onCreate之后使用以上代码
Try postDelayed or Timer method 尝试postDelayed或Timer方法
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
//Intent i = new Intent("first app package","first app class name" );
Intent i = new Intent (this , SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}, 100);
// Time method
import java.util.Timer;
...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Your database code here
}
}, 2*60*1000);
Try this one: 试试这个:
try{
Thread.sleep(3000);
startActivity(secondActivityIntent);
}catch(Exception e){
//print error here
}
You want the second application to hide after few seconds, Please put this code inside onCreate()
of your second application's main Activity
: 您希望第二个应用程序在几秒钟后隐藏,请将此代码放入第二个应用程序的主Activity
onCreate()
内:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
YourActivity.this.finish();
}
}, 3000);
you can use thread but main thing is you have to call finish() method to finish current activity. 您可以使用线程,但主要的是必须调用finish()方法来完成当前活动。
Thread thread = new Thread( new Runnable() {
@Override
public void run() {
try
{
Thread.sleep(3000);
}
Intent intent = new Intent(1stActivity.this, SecondActivity.class);
startActivity(intent);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
finish();
}
}
});
thread.start();
in the try block i put Thread.sleep(3000) you can change it do your work in try block 在try块中,我将Thread.sleep(3000)更改为try块中的工作
handler.post(new Runnable() {
@Override
public void run() {
//Your Code here
}
});
Put this code inside run method of your timer task.And it will work.Hope it will help.Thanks. 将此代码放在您的计时器任务的运行方法中,它将起作用,希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.