如何在特定时间段后开始活动? [英] 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>
推荐答案
可以使用Handler
类的postDelayed()
方法来执行这个:
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);
其中 1000L 是 Runnable 类中的代码之后的时间(以毫秒为单位)将被调用.
Where 1000L is the time in milliseconds after which the code within the Runnable class will be called.
尝试使用这个.
这篇关于如何在特定时间段后开始活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文