2012-03-20 57 views
1

好了第二頁,所以我使用本教程創建了一個非常簡單的啓動畫面:安卓:初始屏幕重新打開應用程序,即使閃屏時,應用程序是退出

http://p-xr.com/android-tutorial-how-to-make-a-basic-splash-screen/

的問題是,如果用戶點擊主頁按鈕或後退按鈕以在啓動屏幕持續時間期間退出應用程序,然後如果用戶沒有退出,則啓動屏幕完成後應用程序將重新打開到第二屏幕。

我的代碼幾乎是教程。任何幫助?

感謝

+0

「錯」?以什麼方式? – LiverpoolFTW 2012-03-20 17:35:22

+0

在他們獲得無縫用戶體驗的方式中出現錯誤,通常在功能上毫無意義,大部分時間只是一個計時器等待(並讓用戶等待) – njzk2 2012-03-20 17:39:44

回答

2

我已經修改了代碼,使用的生命週期方法更好。喜歡改變它。 :)

public class SplashScreen extends Activity { 

     protected int _splashTime = 5000; 

     private Thread splashTread; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash); 


      final SplashScreen sPlashScreen = this; 

      // thread for displaying the SplashScreen 
      splashTread = new Thread() { 
       @Override 
       public void run() { 
        try {     
         synchronized(this){ 
          wait(_splashTime); 
         } 

        } catch(InterruptedException e) {} 
        finally { 

         if(!isFinishing()) // This pretty useful boolean val tells if 
//user has pressed the back button. very useful. 
         {Intent i = new Intent(SplashScreen.this, Main.class); 

         startActivity(i); 
         finish(); 
         } 


         stop(); 
        } 
       } 
      }; 

      splashTread.start(); 
     } 
     @Override 
     public boolean onTouchEvent(MotionEvent event) { 


      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       Toast.makeText(this,"exec---",Toast.LENGTH_LONG).show(); 
       synchronized(splashTread){ 
        splashTread.notifyAll(); 
       } 
      } 
      return true; 
     } 
     @Override 
     protected void onPause() { 

      super.onPause(); 

      if(splashTread.getState()==Thread.State.TIMED_WAITING){ 
       //Thread is still waiting and Activity is paused. Means user has pressed Home. Bail out 
       finish(); 
      } 

     } 
    } 

我的觀點是,使用閃屏並不頻繁,但也許需要。如果你在屏幕後面進行繁重的工作(比如遊戲)。

+0

非常感謝。截至目前爲止的初始屏幕是無用的(我明白爲什麼人們不喜歡它們),但我的推理是這樣的:1.我正在合作的人想要它2.我可能需要那段時間來從服務器(很多圖像,因此可能需要一些時間)。另外,我是新的應用開發,並認爲這將是有趣的實施一個,並知道如何 – LiverpoolFTW 2012-03-20 17:54:02

+0

歡迎。如果數據巨大,可能需要這樣做。雖然在加載圖片的情況下,請選擇「延遲加載圖片」(Romain Guy,Android Shelves或其他),以便用戶在加載圖片時可以繼續使用您的應用。 – Akhil 2012-03-20 18:02:56

+0

感謝您的建議,生病了看。 – LiverpoolFTW 2012-03-20 18:08:16

相關問題