Android啟動—-splash應用?

來源:互聯網
上載者:User

 廣大博友,看過後幫忙頂頂,謝謝大家!!!

 轉載請註明:http://blog.csdn.net/richway2010/article/details/6582818

【博主:各位博友,網友們,大家網上好!歡迎光臨本部落格。 歡迎多多交流,多提意見,互相學習,互相進步,我們的口號是:好好學習,天天向上。】

很多應用程式,進入主程式都需要一個過程,這個過程中如果什麼都不做,給使用者的體驗會不怎麼友好,所以為了更好的使用者體驗,我們需要加上splash,讓使用者在等在的過程中,知道程式在運行,下面我就講一個簡單的例子,在應用中如何使用splash。例子比較簡單,實踐中會複雜的多,希望能指引你,好了,不多說,直接DEMO:

首先建立工程SplashTest,

在建立的工程中增加一個HelloWorldActivity.java並在manifest.xml中定義activity,增加一個頁面splash.xml

SplashTestActivity.java源碼:

package org.guocheng.splash;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;public class SplashTestActivity extends Activity {private final long SPLASH_LENGTH = 3000;Handler handler = new Handler();    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.splash);                handler.postDelayed(new Runnable() {@Overridepublic void run() {Intent intent = new Intent(SplashTestActivity.this, HelloWorldActivity.class);startActivity(intent);finish();}}, SPLASH_LENGTH);//3秒後跳轉    }}

HelloWorldActivity.java源碼:

package org.guocheng.splash;import android.app.Activity;import android.os.Bundle;public class HelloWorldActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);}}

splash.xml源碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent">        <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="splash page....."    /></LinearLayout>

下面看一下運行:

運行進入:

3秒後:

ok!

知識點總結:

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 類的更詳細可以看這篇文章:
下面兩行代碼啟動一個新的Activity,同時關閉當前Activity。
SplashActivity.this.startActivity(mainIntent); 
SplashActivity.this.finish(); 

活動狀態圖:

如上所示,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。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.