[英]start an activity from a TimerTask class
我如何在另一個班級開始一項活動。 在日食中。 一段時間后僅用於啟動Main活動。
public class FirstShow extends ActionBarActivity {
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_show);
Timer tm;
tm = new Timer();
ttask task = new ttask();
tm.schedule(task, 30000);
}
}
class ttask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
FirstShow.class.startActivity());
}
}
我想在幾秒鍾后開始另一個活動。 我怎樣才能做到這一點?
Friend if you want normally want to start activity after few second than use my code:----->
Thread thread = new Thread() {
public void run() {
try {
sleep(3*1000);
Intent i=new Intent(getBaseContext(),ManuList.class);
startActivity(i);
finish();
} catch (Exception e) {
}
}
};
thread.start();
}
public class FirstShow extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_show);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(FirstShow.this,secondactivity.class);
startActivity(i);
}
}, 3000);
}
}
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(FirstActivity.this,secondactivity.class);
startActivity(i);
}
}, 3000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
public class secondactivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
}
}
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.teststart.FirstActivity"
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="com.example.teststart.secondactivity"
android:label="@string/app_name" />
</application>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.