2012-06-14 96 views
2

你好,我想一個默認後顯示的其它閃屏,如果應用程序首次啓動時間(例如安裝後右)閃屏 - 與共享偏好

第一時間,所以我寫了這個。但是新的活動不會啓動,它停留在啓動屏幕上。有人可以說它有什麼問題嗎?

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.widget.TextView; 

    public class splash extends Activity { 
     private Thread splashTread; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash); 

      SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
      if(!prefs.getBoolean("firstTime", false)) { 
       // run your one time code 
       Intent i = new Intent(splash.this, main.class); 

         startActivity(i); 
       SharedPreferences.Editor editor = prefs.edit(); 
       editor.putBoolean("firstTime", true); 
       editor.commit(); 


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

           //wait 2 sec 
           wait(2000); 
         } 

        } catch(InterruptedException e) {} 
        finally { 
         finish(); 



         //start a new activity 
         Intent i = new Intent(); 
         i.setClass(splash.this, main.class); 
           startActivity(i); 

         stop(); 
        } 
       } 
      }; 

      splashTread.start(); 

     } 

    } 
    } 

謝謝。

+0

您確定您在調用的活動中調用了setContentView嗎? – Guardanis

+0

是的。這不應該是問題 – Ahmad

回答

6

從我所看到的,無論啓動是否第一次,您的代碼都會運行相同的Activity(main)。我假設您的目的是在第一次啓動時立即啓動備用閃屏,否則在2秒後繼續執行主活動。另外,我建議使用Handler而不是Thread,因爲你只使用它一次,並且延遲。試試這個:

public class splash extends Activity 
{ 
    private Handler handler = new Handler() 
    { 
     public void handleMessage(Message msg) 
     { 
      Intent i = new Intent(splash.this, main.class); 
      splash.this.startActivity(i); 
           this.finish() 
     } 
    }; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     if(!prefs.getBoolean("first_time", false)) 
     { 
      SharedPreferences.Editor editor = prefs.edit(); 
      editor.putBoolean("first_time", true); 
      editor.commit(); 
      Intent i = new Intent(splash.this, otherSplash.class); 
      this.startActivity(i); 
           this.finish(); 
     } 
     else 
     { 
      this.setContentView(R.layout.splash); 
      handler.sendEmptyMessageDelayed(0, 2000); 
     } 

    } 
} 
+0

非常感謝。爲我工作! – Ahmad

1

而不是調用finish()的......剛開始的主要活動與FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

這裏有該標誌

FLAG_ACTIVITY_CLEAR_TOP in Android

更清晰的詳細信息,我是什麼這就是你要麼

  1. 使Splash成爲默認活動,並在那裏在超時後調用Main活動,或者如果已經看到飛濺(首選項檢查),則爲OR。 (即所有飛濺的邏輯是在Splash活動)
  2. Main活動檢查,看是否Splash應該叫(偏好檢查),如果是使用相同的CLEAR_TOP標誌啓動它,然後有Splash超時和設置Main幾秒鐘後再次使用CLEAR_TOP。 (此混合飛濺邏輯在SplashMain兩者)​​

最終的結果是,Main將在堆棧上唯一的活動,一旦Splash完成。

+0

這實際上不會做任何事情,因爲新打開的活動將位於頂部:splash-> main。當飛濺低於主體時,它不會被這個標誌關閉。使用FLAG_ACTIVITY_CLEAR_TOP的 – Xono

+0

將使新活動成爲堆棧上的唯一活動,並且它是否通過Main - > Splash - > Main或Splash - > Main啓動並不重要,最終結果是Main是唯一活動堆棧。 – stuckless

+0

呃,沒有。我建議你重讀一下你發佈給android開發者的鏈接:如果你有堆棧A,B,C,D,並且D用這個標記調用B的類的startActivity,堆棧變成A,B - 不僅僅是B。殺死比堆疊中開始的活動更高的活動,而不是所有其他活動。 – Xono

0

我不認爲你可以/應該從不是UI線程的線程開始活動。更改爲AsyncTask或使用處理程序

0

如果你的意圖是簡單地擁有多個「撲通」的屏幕,並且他們有沒有邏輯對他們來說,不僅僅是顯示內容等,那麼爲什麼不直接使用一個單一的活動和替換的觀點第二次飛濺的新觀點。您必須在UI線程上執行此更新,因此您需要使用Handler或使用AsyncTask,或使用當前視圖中已知視圖元素的View.post()