2016-07-30 67 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> 

上面的代码肯定的工作,我已经测试。

相关问题