Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put some delay in calling an activity from another activity?

I have an application in which I'm receiving a sms containing his location.On receiving sms it calls another activity to start and passes that location to that activity to plot it on the map.Before calling the second activity it shows a toast like notification on the screen but somehoe due to calling second activity that toast doesn't come up.My question is how can we delay the calling of second activity from this activity ?

like image 603
dark_shadow Avatar asked Nov 01 '11 11:11

dark_shadow


People also ask

How to call another activity from the current activity?

Use the Call instruction to cause the current activity to find another specified activity and execute it. When that activity completes, control returns to the calling activity. The calling activity can pass parameter values to the called activity in two ways.

How does the call instruction in an activity work?

The Call instruction in an activity examines the In/Out value of each parameter in the called activity: If the parameter is declared as Out and the entire parameter page of the current activity is shared with the calling activity, then the value of the Out parameter in the call may be of the form param.name.

How to schedule code to run once after a specified delay?

This interface can schedule code to run once after a specified delay or at fixed time intervals. To run a piece of code once after a delay, we can use the schedule method: The Classname::someTask part is where we specify the method that will run after the delay: To run a task at fixed time intervals, we can use the scheduleAtFixedRate method:

How to implement delays in Java?

This tutorial will describe two ways to implement delays in Java. 2. A Thread -Based Approach When a Java program runs, it spawns a process that runs on the host machine. This process contains at least one thread – the main thread – in which the program runs.


3 Answers

You can use something like this:

 new Handler().postDelayed(new Runnable() {
                      @Override
                      public void run() {

                          Intent i=new Intent(SearxhJobs.this,JobsTypes.class);
                          startActivity(i);
                      }
                  }, 5000);

Here it waits upto 5 seconds to launch activity.

Hope it helps

like image 63
Udaykiran Avatar answered Oct 16 '22 20:10

Udaykiran


You can do it with a Handler like this

    Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            Intent i = new Intent().setClass(ctx, MainActivity.class);                  
            startActivity(i);
        }           
    };

    h.sendEmptyMessageDelayed(0, 1500); // 1500 is time in miliseconds
like image 8
Mats Hofman Avatar answered Oct 16 '22 20:10

Mats Hofman


Make an AsyncClass that does Thread.sleep() in the doInBackground() method, then navigate to your new activity in the your onPostExecute() method.

Call your toast message and then execute the AsyncClass.

like image 3
NotACleverMan Avatar answered Oct 16 '22 19:10

NotACleverMan