0

I am developing and android application in that I want to start another application(my second application). I am doing the following code

 Intent i= new Intent();
 i.setComponent(new ComponentName("my second app package","my class name"));
 startActivity(i);

It is working fine.

I want to hide the second application after 3 or 5 seconds for that I am following the below code

 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 is not working as I expected only for my second application. But other applications are working fine. Instead thread I also tried Handler, alaram manager also no sucess. Please any one help me in this.

Is I want to do any code change in my second application or what is the problem with my code?

Thanks

Krishna
  • 4,812
  • 17
  • 60
  • 96

6 Answers6

4
   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

virendrao
  • 1,660
  • 2
  • 21
  • 39
1

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);
Mina Fawzy
  • 19,665
  • 16
  • 125
  • 141
0

Try this one:

try{
    Thread.sleep(3000);
    startActivity(secondActivityIntent);
}catch(Exception e){
   //print error here
}
The Well
  • 866
  • 1
  • 9
  • 21
0

You want the second application to hide after few seconds, Please put this code inside onCreate() of your second application's main Activity:

new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
            YourActivity.this.finish();
     }
 }, 3000);
M-Wajeeh
  • 16,948
  • 10
  • 63
  • 98
0

you can use thread but main thing is you have to call finish() method to finish 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 put Thread.sleep(3000) you can change it do your work in try block

Adnan Ali
  • 772
  • 1
  • 8
  • 21
0
  handler.post(new Runnable() {

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

            });

Put this code inside run method of your timer task.And it will work.Hope it will help.Thanks.

Nirav Tukadiya
  • 3,237
  • 1
  • 17
  • 35