2

How do you start an activity for a few seconds and then start another activity. I tried this code,when I try the app, it directly starts my MainActivity:

 Thread timer=new Thread() {
        public void run() {

            try {

                sleep(5000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            } finally {

            }
            Intent intent = new Intent("app.my.com.newapp.MAINACTIVITY");
            startActivity(intent);

        }
    };
timer.start();

And here its my manifest file.

<?xml version="1.0" encoding="utf-8"?>

<supports-screens
    android:anyDensity="true"
    android:largeScreens="true"
    android:normalScreens="true"
    android:resizeable="true"
    android:smallScreens="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/danger"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Introduction"
        android:label="@string/title_activity_introduction">
   <intent-filter>
       <action android:name="app.my.com.newapp.MAINACTIVITY"/>
       <category android:name="android.intent.category.DEFAULT"/>


   </intent-filter>

    </activity>
</application>

Please help me out!!

AppGeek
  • 137
  • 4
  • 13

4 Answers4

0

Use the below code to create a splash screen.

public class SplashActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_layout);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashActivity.this, LoginScreenActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    },SPLASH_TIME_OUT);
}

}

Mr Nice
  • 524
  • 8
  • 24
0

You need to modify your manifest to see your first activity, your launcher activity should be Introduction and the other activity tag will define your MainActivity. It seems you want to create a splashscreen, here is a link

which might help you.

lcw_gg
  • 679
  • 5
  • 14
0

rename your Activity's name to the other and put the timer thread code in onStart() of Introduction Activity. then all will do as you want to be.

xiaominglui
  • 349
  • 3
  • 8
0

We could connect a Handler to our activity and post a Runnable via the Handler.

public class MyActivity extends Activity {
    private final Handler mHandler = new Handler();

    private Runnable timedLaunch = new Runnable() {
        public void run() {
            // call the intent from here
        }
    };

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
        // launch after 5 seconds
        mHandler.postDelayed(timedLaunch , 5000);
    }


    // be sure to remove the callback in case the user leaves the activity before the the given number of seconds have passed

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mTask);
    }
}

Or we could use a TimerTask(java.util.TimerTask) like this:

class MyTimerTask extends TimerTask {

  @Override
  public void run() {
    // launching code goes here
  }

 }

Then create the timer to be scheduled for a given delay:

MyTimerTask myTimerTask = new MyTimerTask();
java.util.Timer timer = new java.util.Timer();

// runs after 5 seconds
timer.schedule(myTimerTask, 5000);
ZakiMak
  • 2,072
  • 2
  • 17
  • 26