2011-12-28 69 views
2

目前我正在開發一個android-phonegap項目。AsyncTask運行時的Android Splashscreen

在應用程序加載index.html之前,有一些本機代碼正在工作。詳細地說,它是在AsyncTask中敲擊SD卡(如果有的話)。

我設法顯示一個進度條,而A.Task正在工作,但此外我想在後臺添加一個閃屏。我主要與Phonegap合作,並開始使用本機代碼。所以我有點困惑於所有這些佈局,主題和你可能在.xml文件中定義的其他東西。我非常確定這對於更大的UI設計來說也是一個好主意,但對於我現在想要的簡單的Splash屏幕來說,這感覺就像是一次徹底的矯枉過正。

這是源碼片段。直接向前。 onCreate()調用AsyncTask,它做了一些工作,並在其PostExecute方法中啓動PhoneGap。我希望屏幕出現在onCreate方法或onPreExecute中。作業完成後,屏幕將關閉onPostExecute()中的屏幕。我還添加了評論來說明我的想法。

public class myAct extends DroidGap { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Show Splash here or in onPreExecute()! 
     new myAsTa().execute(); 
    } 
} 


class myAsTa extends AsyncTask<Void, Integer, Boolean> { 
    ProgressDialog dialog = new ProgressDialog(myAct.this); 

    @Override 
    protected void onPreExecute() { 
     //or show splash here! 
     dialog.setMessage("Cool Progressbar!"); 
     dialog.show(); 
     super.onPreExecute(); 
    } 

    protected Boolean doInBackground(Void... values) { 
      //magician at work! 
      return true; 
     } 


     @Override 
     protected void onProgressUpdate(Integer... values) { 
      //insult user here 
     } 

     protected void onPostExecute(Boolean result) { 
      dialog.dismiss(); 
      //dismiss splashscreen 
      //start Phonegap 
     } 
    } 

感謝您的閱讀和您的幫助:)

回答

5

我曾經做過這樣一個閃屏here。這會將飛濺佈局本身,當應用程序被加載(AndroidManifest.xml)什麼是顯示在屏幕上

<activity android:name=".SplashActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 

setContentView(R.layout.splash);是指直接從系統中觸發 - 這個佈局基本

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="@drawable/desktop_rhq" 

     > 

    <TextView android:layout_gravity="bottom" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:text="@string/loading" 
      /> 
</LinearLayout> 

哪定義了一個背景圖片和一些文字。說實話,我並沒有想太多方向改變,而飛濺正在運行 - 我想這會重新開始後臺任務。

+0

請問您可以添加一點點信息嗎?這看起來不錯,但我不是100%確定如何使用它。佈局中是否定義了飛濺?你是否也可以展示這是如何完成的,因爲我從未使用佈局? – yoshi 2011-12-28 10:16:50

+0

...以及它如何改變方向? – Selvin 2011-12-28 10:17:44

+0

謝謝。對我來說工作得很好。雖然我看不出你的代碼和我的方法有什麼區別。 :)然而拯救了我的一週。對於方向更改,我很好,因爲它已被應用程序阻止^ .- – yoshi 2011-12-29 11:30:40

1

splash.xml

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:src="@drawable/splash_image"> 

</ImageView> 

SplashActivity.class

public class SplashActivity extends Activity { 

     protected boolean _active = true; 
     protected int _splashTime = 1000; // time to display the splash screen in ms 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash); 

     // thread for displaying the SplashScreen 
      Thread splashTread = new Thread() { 
       @Override 
       public void run() { 
        try { 
         int waited = 0; 
         while(_active && (waited < _splashTime)) { 
          sleep(100); 
          if(_active) { 
           waited += 100; 
          } 
         } 
        } catch(InterruptedException e) { 

        } finally { 

         startActivity(new Intent(SplashActivity.this, NextActivity.class)); 
         finish(); 
         //stop(); 
        } 
       } 
      }; 
      splashTread.start(); 
     } 
    } 

作用於方向的變化:i將在AndroidManifest.xml screenOrientation="portrait"

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

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

我不喜歡有固定的時間顯示飛濺的想法。這只是在收到數據的第一時刻,所以我寧願保持儘可能短的=) – yoshi 2011-12-29 11:32:46