2016-07-30 63 views
0

如何在用戶安裝我的應用程序時顯示啓動畫面。每次用戶打開時,我都不想顯示啓動畫面,但只有在USER將其安裝在手機上並第一次打開時才顯示啓動畫面。如何實現這一目標?如何在App上顯示一次Splash Screen?

+3

可能的重複[如何使閃屏?](http://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen?rq=1)或[如何僅當應用程序啓動「新鮮」時才顯示啓動畫面?](http://stackoverflow.com/questions/7682439/how-to-show-splash-screen-only-when-the-app-starts-fresh?rq = 1) –

+1

您可以使用相同的共享首選項。 –

回答

1

你需要檢查每次當應用程序將打開,這是它的第一次啓動的應用程序?如果是,那麼顯示你的一次閃屏其他顯示主要​​活動

你可以使用共享首選項來存儲有關第一次啓動的數據。

0

在您每次啓動應用程序時都會顯示的主要活動中,請嘗試以下邏輯。

SharedPreferences mPrefs; 
final String splashScreenPref= "SplashScreenShown"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this); 


    Boolean splashScreenShown= mPrefs.getBoolean(splashScreenPref, false); 

    if (!splashScreenShown) {    
     Intent intent=new Intent(MainActivity.this,SplashScreenActivity.class); 
     startActivity(intent); 

     SharedPreferences.Editor editor = mPrefs.edit(); 
     editor.putBoolean(splashScreenShown, true); 
     editor.commit(); 
     finish(); 
    } 

} 

請參考以下鏈接瞭解如何使用SharedPreferences更好的理解:link1link2link3

0

這裏,

您應該創建應用類別及需要打電話給你需要從應用程序類活動onCreate方法。

public class Appli extends android.app.Application { 
     @Override 
     public void onCreate() { 
      super.onCreate(); 
//manage base on your requirement,you can use share preference for splash screen track 
      Intent intent = new Intent(this,Main2Activity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      startActivity(intent); 

     } 

清單:

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:name=".Appli" 
     android:theme="@style/AppTheme"> 
</application> 

上面的代碼肯定的工作,我已經測試。

相關問題