How to start an activity after certain time period

2019-01-12 03:16发布

i need to start an activity from the current activity after a certain time period. I coded like below.

public class FirstActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);


    new Timer().schedule(new TimerTask(){
        public void run() { 
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
        }
    }, 2000); 
}

But its not working..it keeps on crashing.. Is my method correct? my manifest file is as below

`

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_first" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.first.FirstActivity" />
    </activity>
</application>

标签: android timer
8条回答
倾城 Initia
2楼-- · 2019-01-12 03:56
@Override
protected void onStart() {
    super.onStart();

    try {
    Runnable myRunnable = new Runnable(){

        public void run(){

        try{
           sleep(SPLASH_TIME_OUT);
            finish();

        }catch (Exception e){
            System.exit(1);
        }
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);
        }
    };

    Thread thread = new Thread(myRunnable);
    thread.start();




    }catch (Exception e){
        System.exit(1);
    }

}
查看更多
仙女界的扛把子
3楼-- · 2019-01-12 03:59

You can use the Handler class postDelayed() method to perform this:

Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {

    @Override
    public void run() {
        //start your activity here  
    }

}, 1000L);

Where 1000L is the time in milliseconds after which the code within the Runnable class will be called.

Try to use this .

查看更多
萌系小妹纸
4楼-- · 2019-01-12 04:01

Can you try this:

Thread toRun = new Thread()

        {
               public void run()
               {
                   try {                      
                    sleep(500);

                     Intent intent = new Intent (FirstActivity.this, SecondActivity.class);
                     startActivity(intent);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               }
        };
        toRun.start();
查看更多
【Aperson】
5楼-- · 2019-01-12 04:04

Thanks all... its working now.. the problem is with the second activity not with the timer. when i comment out "getActionBar().setDisplayHomeAsUpEnabled(true);" these lines in the second activity, it started working . these lines wont give any error at compile time but, during runtime it will be an issue.Thanks.

查看更多
地球回转人心会变
6楼-- · 2019-01-12 04:06

I found this on the net. You have that create a interface and you implement it in you class in a method anonymous

public static void delay(int timeMillisecond , final DelayCallBack delayCallBack){
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            delayCallBack.postDelay();
        }
    } , timeMillisecond );
}

public interface DelayCallBack{
    void postDelay();
}

Util, That's where I have my static methods

Util.delay( delay , new Util.DelayCallBack() {
    @Override
    public void postDelay() {
        startActivity( new Intent( [currentContext] , [nextActivity] );
    }
});
查看更多
混吃等死
7楼-- · 2019-01-12 04:07

I don't know what is wrong with your code but this should work:

private Handler mHandler = new Handler();
mHandler.postDelayed(mStartActivityTask, 2000);
private Runnable mStartActivityTask= new Runnable() {
    public void run() {
       // Start the Activity
    }
};
查看更多
登录 后发表回答