可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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>
回答1:
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 .
回答2:
use runOnUiThread for starting activity from Timer as:
new Timer().schedule(new TimerTask(){
public void run() {
FirstActivity.this.runOnUiThread(new Runnable() {
public void run() {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}, 2000);
回答3:
Give this a try. I use this for a Splash Screen in my app and works just fine. Also, as @Venture pointed out in the comment, make sure the activity is added to your manifest file.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent startActivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(startActivity);
finish();
}
}, 2000);
回答4:
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] );
}
});
回答5:
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
}
};
回答6:
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();
回答7:
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.
回答8:
@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);
}
}