Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

android - How to start an activity after certain time period?

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>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...