Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call another activity after certain time limit

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.

like image 368
RAAAAM Avatar asked Mar 30 '11 11:03

RAAAAM


People also ask

How to call a function after a certain delay in time?

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.

How to call a function after some interval in Python?

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 ...

How to break a loop if the time has exceeded 30 seconds?

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:

How to schedule a task in Python with a timer?

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.


Video Answer


2 Answers

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.

like image 85
RoflcoptrException Avatar answered Oct 18 '22 19:10

RoflcoptrException


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);
    }
}
like image 35
james Avatar answered Oct 18 '22 19:10

james