2013-12-21 36 views
0

我目前正在開發一個項目,其中SplashScreen需要啓動AsyncTask#1,然後AsyncTask#2AsyncTask#5。一旦完成,它應該將用戶帶到HomeActivity按順序運行AsyncTasks並共享相同的佈局

AsyncTask#1加載所有其他任務所需的數據。此外,我需要AsyncTask#2AsyncTask#5共享相同的佈局(一個ImageView +一個TextView)。

基本上,就像一個加載屏幕:TextView: "Loading items"ImageView: item currently being loaded。即使圖像快速滾動,我仍然不得不讓用戶看到圖像加載/更改。

我寫的手以下,所以忽略語法錯誤,如果有的話:/


目前,我splashScreen看起來是這樣的:

try { 
    DatabaseSetup async1 = new DatabaseSetup(this); 
    async1.execute().get; 
    new Async2.execute(); 
    new Async3.execute(); 
    new Async4.execute(); 
    new Async5.execute(); 
} finally { 
    Intent i = new Intent(this, HomePage.class); 
    StartActivity(i); 
} 


Async2 to 5

class Async# extends AsyncTask<Void, String, Boolean> { 
    private Context context; 
    private Activity activity; 
    private TextView tv; 
    private ImageView iv; 

    public Async#(Context context) { 
     this.context = context; 
     this.activity = (Activity) context; 
    } 

    onPreExecute() { 
     super.onPreExecute(); 
     tv = (TextView) activity.findViewById(R.id.tvLoader); 
     iv = (ImageView) activity.findViewById(R.id.ivLoader); 
    } 

    doInBackground() { 
     publishProgress("Running Async#"); 
     . 
     . 
     . 
     for(int i=0; i < items.length; i++) { 
      publishProgress(item[i].getImageLink()); 
     } 
    } 

    onProgress(String... values) { 
     if(values[0].contains("Loading") 
      tvLoader.setText(values[0]); 
     else() 
      ivLoader.setImageURL(values[0]); 
    } 
} 

那麼,這樣做的正確方法是什麼?我在某處讀到,如果我使用async1.execute().get(),則只有在Async1完成後纔會調用以下行。我完全困惑。此代碼有效,但我有時會得到Surface#2 is not valid.

我不確定是否需要使用充氣器? ImageView實際上是一個從Loopj(SmartImageView)獲取的庫,用於緩存我得到的圖像。

的logcat:

12-21 16:45:16.722: E/ViewSystem(8941): ViewRootImpl #2 Surface is not valid. 
12-21 16:45:16.772: D/AbsListView(8941): Get MotionRecognitionManager 
12-21 16:45:16.782: D/AbsListView(8941): onVisibilityChanged() is called, visibility : 4 
12-21 16:45:16.782: D/AbsListView(8941): unregisterIRListener() is called 
12-21 16:45:16.782: D/AbsListView(8941): onVisibilityChanged() is called, visibility : 0 
12-21 16:45:16.782: D/AbsListView(8941): unregisterIRListener() is called 
12-21 16:45:16.852: D/libEGL(8941): loaded /system/lib/egl/libEGL_adreno200.so 
12-21 16:45:16.852: D/libEGL(8941): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
12-21 16:45:16.862: D/libEGL(8941): loaded /system/lib/egl/libGLESv2_adreno200.so 
12-21 16:45:16.862: I/Adreno200-EGL(8941): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: HAREESHG_Nondeterministic_AU+PATCH[ES]_msm8960_JB_1.9.6_MR2_CL3219408_release_ENGG (CL3219408) 
12-21 16:45:16.862: I/Adreno200-EGL(8941): Build Date: 09/28/13 Sat 
12-21 16:45:16.862: I/Adreno200-EGL(8941): Local Branch: hhh 
12-21 16:45:16.862: I/Adreno200-EGL(8941): Remote Branch: quic/jb_1.9.6_1 
12-21 16:45:16.862: I/Adreno200-EGL(8941): Local Patches: 8d50ec23e42ef52b570aa6ff1650afac0b503d78 CL3219408: Fix in the Glreadpixels for negative offsets and larger dimensions. 
12-21 16:45:16.862: I/Adreno200-EGL(8941):     801859126f6ca69482b39a34ca61447e3f7cded8 rb: fix panel settings to clear undrawn/undefined buffers 
12-21 16:45:16.862: I/Adreno200-EGL(8941): Reconstruct Branch: LOCAL_PATCH[ES] 
12-21 16:45:16.902: D/OpenGLRenderer(8941): Enabling debug mode 0 
12-21 16:45:16.932: D/AbsListView(8941): unregisterIRListener() is called 
12-21 16:45:16.952: D/AbsListView(8941): unregisterIRListener() is called 
12-21 16:45:17.022: D/AbsListView(8941): unregisterIRListener() is called 
12-21 16:45:17.052: D/AbsListView(8941): unregisterIRListener() is called 

非常感謝您閱讀這一切!

+0

表面#2的含義無效。 !如果有異常提出,請分享給您logcat –

+0

它不會引發異常。我將使用LogCat更新問題,儘管 –

+0

是否應該在第5個AsyncTask完成後啓動(home.class)? –

回答

1

您可以使用簡單的類信號量法。 讓你飛濺的活動來實現一個簡單的界面:

public interface DecrementCallback{ 
    public void setCount(int cnt); 
    public void onDecrementCount(); 
} 

定義一個成員作爲計數器:

private int cnt = 0; 

你的方法來實現應該是這樣的:

 @Override 
     public void setCount(int cnt) { 
      this.cnt = cnt; 
     } 

     @Override 
     public void onDecrementCount() { 
      cnt--; 
      if(cnt <= 0){ 
       /** 
       * Start your main activity 
       */ 
      } 
     } 

開始之前asyntasks,設置計數器值:

setCount(5); 

此方法添加到您的asynctasks:

@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    ((DecrementCallback)activity).onDecrementCount(); 
} 

你應該很清楚的,當你分享所有asynctasks中相同的TextView和ImageView的你做了什麼,因爲如果你不這樣做,你可以混淆用戶正確更新。

編輯:

如果你想連續運行asynctasks(第一Async1,然後Asyn2,然後Async3),你可以修改onPostExecute()方法是這樣的:

@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 
    ((DecrementCallback)activity).onDecrementCount(); 

      if(this instanceof Async1){ 
      new Async2(activity).execute(); 
    }else if(this instanceof Async2){ 
     new Async3(activity).execute(); 
    }else if(this instanceof Async3){ 
     new Async4(activity).execute(); 
    }else if(this instanceof Async4){ 
     new Async5(activity).execute(); 
    } 
} 

然後,您只需在您的活動中啓動Async1。

編輯2:

如果你想開始從飛濺活動的所有異步任務,您可以修改回調是這樣的:

public static interface DecrementCallback{ 
    public void setCount(int cnt); 
    public void onDecrementCount(AsyncTask<Void, Void, Void> task); 
} 

的的的AsyncTask方法應該是:

@Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     ((DecrementCallback)activity).onDecrementCount(this); 
    } 

而在你的活動覆蓋方法應該是這樣的:

@Override 
     public void onDecrementCount(AsyncTask<Void, Void, Void> task) { 
      cnt--; 
      if(cnt <= 0){ 
       /** 
       * Start your main activity 
       */ 
      } 

      if(task instanceof Async1){ 
       new Async2(activity).execute(); 
      }else if(task instanceof Async2){ 
       new Async3(activity).execute(); 
      }else if(task instanceof Async3){ 
       new Async4(activity).execute(); 
      }else if(task instanceof Async4){ 
       new Async5(activity).execute(); 
      } 
     } 

您有很多變體暴露。希望他們能夠有用。

問候。

+0

好吧,這將允許我開始HomePage活動。但是,這將如何讓我順序運行AsyncTasks? –

+0

你是否需要依次或平行運行你的異步?您發佈的代碼適用於所有這些代碼並行運行。 –

+0

我需要他們順序運行(在標題中說:P)。 –