2012-01-08 71 views
4

我想在我的應用程序中實現一個SplashScreen。我發現最好也最簡單的方法是啓動一個活動,在應用程序啓動時顯示帶有圖像視圖的佈局,然後向清單添加android:noHistory =「true」屬性。 現在,如何設置啓動畫面活動以在一段時間之後啓動MainActivity類?讓我們說2秒?在一段時間之後打開一個活動?

這是我的啓動畫面活動

public class SplashActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splashscreen); 
} 
} 
+0

只是使用標誌沒有歷史的警告 - 該SYSTEN會隨時結束活動的任何其他活動啓動後。這很難控制應用程序代碼。所以不要在no_history活動的破壞函數中釋放任何東西。這會導致意外和難以調試的異常 – png 2012-01-09 08:05:38

回答

4

使用

handler.postDelayed(runnable, delayinmilliseconds(2000 in your case)); 

final Runnable runnable = new Runnable() 
{ 
    public void run() 
    { 
     //start the new activity here. 
    } 
}; 
+0

你可以將你的代碼與我的合併嗎?每當我嘗試時都會收到錯誤。仍然是小菜,對不起! – borislemke 2012-01-08 17:05:08

+0

只是在類中聲明一個處理程序。並在setContentView – 2012-01-08 17:19:23

+0

之後使用我在onCreate()方法中給出的代碼啊,謝謝!作品! – borislemke 2012-01-08 17:37:47

3

下面是一個完整的示例。

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); 
    } 
} 
+0

這也有效!謝謝! – borislemke 2012-01-08 17:38:03

0

您也可以以這種方式使用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*/); 
1
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(); 


} 

}

我希望這將可以解決您的解決方案。

0
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(); 



     } 
相關問題