簡體   English   中英

如何在Android中顯示啟動畫面

[英]How to display Splash Screen in android

我想在Android應用中顯示啟動畫面。 但是我想在初始屏幕后面執行MainActivity的onCreate()方法。 因為我正在用這種方法做大量的工作。 任何人都可以打電話給我該怎么做。

基本上,您想在用戶顯示一些初始屏幕時在后台做一些工作,對嗎? 您需要的是異步任務或裝載程序之類的東西。

步驟1:顯示啟動畫面。 步驟2:啟動異步任務,並在異步任務的doInBackground方法中進行所有繁重的處理。步驟3:使用異步任務的onPostExecute方法更新UI。 在這種方法中,首先關閉計時器以啟動屏幕。 然后發送意圖以異步任務的大量處理結果數據開始另一個屏幕。 在UI線程上顯示它。

僅顯示飛濺屏幕非常簡單。 此代碼創建3秒鍾的啟動屏幕,然后將Intent發送到另一個活動。

public class SplashScreenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);


        CountDownTimer cdt1 = new CountDownTimer(3000, 1000) {

            Boolean checkInternetConnection = false;

            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {

          //Send Intent here
             Intent i = new Intent(getApplicationContext(), 
                         anotherActivity.class);
                startActivity(i);
            }
        }.start();

    }

PS-不要忘記使用清單清單文件中的啟動器活動來執行此代碼。

您可以在啟動畫面中嘗試...

public class SplashScreen extends Activity {

//Further Needed Declarations

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

        /**
         * Showing splashscreen while making network calls to download necessary
         * data before launching the app Will use AsyncTask to make http call
         */
        new PrefetchData().execute(); 
    }
}

這與您想要的方式完全相同。

在drawable中創建文件

    <item
        android:drawable="@color/gray"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

</resources>

活動:

public class SplashActivity extends AppCompatActivity {

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

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

您可以在onCreate方法中添加它

   new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // going to next activity
                    Intent i=new Intent(SplashScreenActivity.this,MainActivity.class);
                    startActivity(i);
                    finish();
                }
            },time);

並根據需要初始化以毫秒為單位的時間值...

private  static int time=5000; 

有關更多詳細信息,請從此鏈接下載完整代碼...

https://github.com/Mr-Perfectt/Splash-Screen

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM