仅在新鲜打开时显示Splashscreen

时间:2017-07-31 11:09:23

标签: android splash-screen

我有一个有效的启动画面活动。但是,如果用户点击后退按钮,并在再次恢复应用程序后,我不想显示启动画面。我怎么能这样做?

SplashScreen.java

public class SplashScreen extends Activity {

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

        Thread t = new Thread() {
            public void run() {
                try {
                    int time = 0;
                    while (time < 4000) {
                        sleep(100);
                        time += 100;
                    }
                }
                catch (InterruptedException e) {
                    // do nothing
                }
                finally {

                    Intent i = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
            }
        };
        t.start();

    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fardrop.radarso">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/app_theme">

        <activity
            android:name="com.fardrop.radarso.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance"
            android:alwaysRetainTaskState="true"
            android:icon="@mipmap/ic_launcher"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
        </activity>

    </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

试试这个

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_splash);
    new Handler().postDelayed(new Runnable() {    
    @Override
    public void run() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
        // close this activity

        /* or this
         Intent intent = new Intent(LoginActivity.this, HomeScreenActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);*/

    }
}, 3000);// time for spalsh screen

答案 1 :(得分:1)

只需在SplashActivity中使用以下代码;

  new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Intent i = new Intent(SplashScreen.this, HomeScreenActivity.class);
                            startActivity(i);
                            finish();
                        }
                        catch (final Exception e)
                        {
                            runOnUiThread(new Runnable(){
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    }
                }, 2*1000);

答案 2 :(得分:0)

您在SharedPreferences中存储可见标记,并在再次显示时从sharedPreferences获取值以显示SplashScreen。 您可以参考以下答案进一步说明 show splash screen one time only