How to give time limit for calling one activity to another activity. I want to call another activity (Ex calling A class to B class) by given some time limit. I used alarmManager, but it is not possible for my application also i used threading, whenever i give Thread.sleep it will not perform action till the sleep getting end. I am not sure how to do this process. can anyone give a idea of giving time limit to calling a class.
This function will get called after a certain delay in time. Moreover, it is necessary to write the function definition above function call. start_time is a timer object that has the arguments of delay_time and function itself. .start ( ) starts the timer. Certainly, the output clearly determines that the code below gets executed first.
Call a function after some interval in Python 1 A timer applies the desired delay. 2 Python has timer objects that provide the facility to give arguments. 3 The timer is basically a subclass of the Thread class. 4 timer.start ( ) starts the timer and schedules the task. More ...
long start = System.currentTimeMillis (); long end = start + 30 * 1000 ; while (System.currentTimeMillis () < end) { // Some expensive operation on the item. } Here, the loop will break if the time has surpassed the limit of 30 seconds. There are some noteworthy points in the above solution:
timer.start ( ) starts the timer and schedules the task. Don’t worry, that module is inbuilt and no extra code is necessary to import that. This is the class that consists of a subclass called timer. Now, writing user-defined function: This function will get called after a certain delay in time.
You could use a Timer and add a TimerTask that is executed after a specific delay.
Here is a more or less completed example:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//here you can start your Activity B.
}
}, 10000);
The example above executes a new TimerTask in 10 seconds. Inside the TimerTask you can override the run method. In the run method you can start your new activity. The run method is executed after the delay. In this example it is 10'000 milliseconds.
Here is a simply solution. this could be used for example for showing a splash activity for 1 second then going into the main app:
public class Splash extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, ActivityB.class));
finish();
}
}, secondsDelayed * 1000);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With