Android啟動畫面Splash

來源:互聯網
上載者:User

方法一,兩個Activity

核心代碼:

package ghj1976.HelloWorld;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;public class SplashActivity extends Activity {private final int SPLASH_DISPLAY_LENGHT = 8000; // 延遲八秒@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.splash);new Handler().postDelayed(new Runnable() {public void run() {Intent mainIntent = new Intent(SplashActivity.this,HelloWorldActivity.class);SplashActivity.this.startActivity(mainIntent);SplashActivity.this.finish();}}, SPLASH_DISPLAY_LENGHT);}}

說明:

Handler().postDelayed  是延遲指定的時間再執行

Handler類主要可以使用如下3個方法來設定執行Runnable對象的時間:

//  立即執行Runnable對象  public final boolean post(Runnable r);  //  在指定的時間(uptimeMillis)執行Runnable對象  public final boolean postAtTime(Runnable r, long uptimeMillis);  //  在指定的時間間隔(delayMillis)執行Runnable對象  public final boolean postDelayed(Runnable r, long delayMillis); 

有關 Handler 類的更詳細可以看這篇文章:http://book.51cto.com/art/201006/207064.htm

下面兩行代碼啟動一個新的Activity,同時關閉當前Activity。

SplashActivity.this.startActivity(mainIntent);

SplashActivity.this.finish();

對 finish 方法的解釋如下: http://android.toolib.net/reference/android/app/Activity.html

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

圖來自: http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/

如上所示,Android 程式員可以決定一個 Activity 的“生”,但不能決定它的“死”,也就時說程式員可以啟動一個 Activity,但是卻不能手動的“結束”一個 Activity。

當你調用 Activity.finish()方法時,結果和使用者按下 BACK 鍵一樣:告訴 Activity Manager 該 Activity 執行個體完成了相應的工作,可以被“回收”。

隨後 Activity Manager 啟用處於棧第二層的 Activity 並重新入棧,同時原 Activity 被壓入到棧的第二層,從 Active 狀態轉到 Paused 狀態。

例如上面例子中:從 SplashActivity 中啟動了 HelloWorldActivity,則當前處於棧頂端的是 HelloWorldActivity,第二層是 SplashActivity 。

當我們調用 SplashActivity.finish()方法時(我們是在SplashActivity中通過SplashActivity.this.finish()調用的),SplashActivity 從 Active 狀態轉換 Stoped 狀態,並被系統從棧中移除,標誌可以被“回收”。

Activity 的狀態與它在棧中的位置關係如:

 

的例子是

從 Activity1 中啟動了 Activity2,則當前處於棧頂端的是 Activity2,第二層是 Activity1,當我們在 Activity2中調用 Activity2.finish()方法時,Activity Manager 重新啟用 Activity1 併入棧,Activity2 從 Active 狀態轉換 Stoped 狀態,同時標註Activity2可以被“回收” 。Activity1. onActivityResult(int requestCode, int resultCode, Intent data)方法被執行,Activity2 返回的資料通過 data參數返回給 Activity1。

 

方法二:一個 Activity

 

布局檔案:

http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent">@+id/splashscreen"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent">@+id/info" android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center"android:paddingTop="10px" android:text="This is a splash !" />fill_parent"android:paddingTop="10px" android:layout_height="wrap_content"android:text="This is a Context" />

說明:

這裡有一個id為splashscreen的LinearLayout,是程式啟動時顯現的部分。當啟動完成後,它會被隱藏。

核心代碼:

package ghj1976.AndroidTest;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.view.View;import android.view.Window;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity {private LinearLayout splash;private TextView tv;private static final int STOPSPLASH = 0;// time in millisecondsprivate static final long SPLASHTIME = 1000;private Handler splashHandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case STOPSPLASH:SystemClock.sleep(4000);splash.setVisibility(View.GONE);break;}super.handleMessage(msg);}};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().requestFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.main);splash = (LinearLayout) findViewById(R.id.splashscreen);tv = (TextView) findViewById(R.id.info);tv.setText("正在建立資料連線");Message msg = new Message();msg.what = STOPSPLASH;splashHandler.sendMessageDelayed(msg, SPLASHTIME);}}

說明

我們在應用啟動後發送一個訊息,把 指定地區設定為隱藏, splash.setVisibility(View.GONE);  就實現了 啟動介面。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.