How to get started after a while in Android?
I am developing an android app and app in that I want to run another app (my second app). I am doing the following code
Intent i= new Intent();
i.setComponent(new ComponentName("my second app package","my class name"));
startActivity(i);
It works fine.
I want to hide the second app after 3 or 5 seconds because I am following the code below
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 doesn't work as I expected for my second application only. But other apps work fine. Instead, I also tried to handle the handler, the alaram manager also failed. Please help me with this.
I want to perform any code change in my second application or what is the problem with my code?
thank
source to share
you can use a stream, but most importantly, you need to call the finish () method to end the current activity.
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 will put Thread.sleep (3000), you can change it, your work is in the try block
source to share
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
source to share
Try PostDelayed or Timer method
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);
source to share