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

0


source to share


6 answers


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

0


source


   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

+2


source


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);

      

+1


source


Try the following:

try{
    Thread.sleep(3000);
    startActivity(secondActivityIntent);
}catch(Exception e){
   //print error here
}

      

0


source


You want the second app to hide after a few seconds, enter this code inside onCreate()

your second main app Activity

:

new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
            YourActivity.this.finish();
     }
 }, 3000);

      

0


source


  handler.post(new Runnable() {

                @Override
                public void run() {
                    //Your Code here
                }

            });

      

Place this code inside the start method of your timer task. And it will work. Hope this helps. Thank.

0


source







All Articles