Android 在一定时间后打开活动?
我想在我的应用程序中实现SplashScreen。我发现最好、最简单的方法是启动一个活动,在应用程序启动时显示带有图像视图的布局,然后将android:noHistory=“true”属性添加到清单中。 现在,如何设置splashscreen活动以在一定时间后启动MainActivity类?比如说2秒钟 这是我的splashscreen活动Android 在一定时间后打开活动?,android,android-activity,timer,splash-screen,Android,Android Activity,Timer,Splash Screen,我想在我的应用程序中实现SplashScreen。我发现最好、最简单的方法是启动一个活动,在应用程序启动时显示带有图像视图的布局,然后将android:noHistory=“true”属性添加到清单中。 现在,如何设置splashscreen活动以在一定时间后启动MainActivity类?比如说2秒钟 这是我的splashscreen活动 public class SplashActivity extends Activity { @Override public void onCreate(
public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
}
使用
这是一个完整的样本
package com.test.splash;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
public class splash extends Activity {
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 3000;a
private ImageView splash;
//handler for splash screen
private Handler splashHandler = new Handler() {
/* (non-Javadoc)
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
splash = (ImageView) findViewById(R.id.splashscreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
您还可以通过以下方式使用
java.util.Timer
:
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
}, 2000 /*amount of time in milliseconds before execution*/ );
公共类TrackMyMoneyActivity扩展活动{
//成员字段
private ProgressBar pbar=null;
私有文本视图计数器_txt=null;
Thread splash_Thread=null;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar=(ProgressBar)findviewbyd(R.id.splashpbar);
counter_txt=(TextView)findViewById(R.id.countertxt);
//定义线程
splash_thread=新线程(new Runnable(){
@凌驾
公开募捐{
//TODO自动生成的方法存根
int i=0;
对于(i=0;我可以将你的代码与我的代码合并吗?我每次尝试都会出错。仍然是一个noob,对不起!只需在类中声明一个处理程序。并使用我在onCreate()中给出的代码方法在SetContentView之后只是一个关于使用标记no history的警告-在启动任何其他活动后,系统将随时完成该活动。这完全超出了应用程序代码的控制。因此,不要释放no_history活动的destroy函数中的任何内容。这将导致意外和难以调试例外情况
new Timer().schedule(new TimerTask(){
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
}, 2000 /*amount of time in milliseconds before execution*/ );
public class TrackMyMoneyActivity extends Activity {
//member fields
private ProgressBar pbar = null;
private TextView counter_txt = null;
Thread splash_thread = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pbar = (ProgressBar) findViewById(R.id.splashpbar);
counter_txt = (TextView) findViewById(R.id.countertxt);
//define thread
splash_thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
for( i=0;i<100;i++){
pbar.setProgress(i);
// counter_txt.setText(i+" %");
try {
splash_thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(i==100){
Intent intent = new Intent(getBaseContext(), LoginApp.class);
startActivity(intent);
}
}
});
splash_thread.start();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public class Splashscreen extends Activity
{
private static final int SPLASH_TIME = 10 * 1000;// 3 seconds
Button logo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splashscreen);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
try {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(Splashscreen.this,MainActivity.class);
startActivity(intent);
Splashscreen.this.finish();
//overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}, SPLASH_TIME);
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, SPLASH_TIME);
} catch(Exception e){}
// METHOD 1
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(50*1000);
// After 5 seconds redirect to another intent
Intent i=new Intent(getBaseContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
background.start();
}