2014-10-01 63 views
0

this線程設置的知名度,我試着與該代碼的變量狀態欄:安卓:從的AsyncTask

private int[] loadingElementIDs; 
private void initLoadingBar() { 
    final DisplayMetrics displayMetrics=getResources().getDisplayMetrics(); 
    final float screenWidthInDp = displayMetrics.widthPixels/displayMetrics.density; 
    final int elementAmount = (int) (Math.floor(screenWidthInDp * 0.5f/30) * 5); 

    //set margins 
    LinearLayout container = (LinearLayout)findViewById(R.id.loading_outer); 
    ... 
    container.requestLayout(); 

    //declare length 
    loadingElementIDs = new int[elementAmount]; 

    LayoutParams LLParams = new LayoutParams(0, LayoutParams.MATCH_PARENT); 
    LLParams.weight = 1f; 
    LinearLayout element; 
    for (int i=0; i<elementAmount; i++) { 
     int id = generateViewId(); //creates unique id 

     element = new LinearLayout(this); 
     element.setVisibility(View.INVISIBLE); 
     element.setLayoutParams(LLParams); 
     element.setBackgroundDrawable(getResources().getDrawable(R.drawable.loading_inner)); 
     element.setId(id); 
     element.requestLayout();    
     container.addView(element); 

     loadingElementIDs[i] = id; 
    } 

} 

,這是對我工作的罰款,但現在我想計算某物的的AsyncTask和使元素可見(我的活動類中的代碼):

private class PrefetchData extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @SuppressWarnings("static-access") 
    @Override 
    protected Void doInBackground(Void... arg0) { 

     try { 
      int step = 0; 
      float totalSteps = 100f; 
      while (...) { 
       step++; 

       // ................... 

       //show status 
       setLoadingStatus(step/totalSteps); 
      }    
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     Intent i = new Intent(SplashScreen.this, MainActivity.class); 
     startActivity(i); 
     finish(); 
    } 

} 

public void setLoadingStatus(float percentage) { 
    int max = (int) Math.min(Math.floor(percentage * loadingElementIDs.length), 
    for (int d=0; d<max; d++) { 
     ((LinearLayout)findViewById(loadingElementIDs[d])).setVisibility(View.VISIBLE); 
     LinearLayout el = (LinearLayout)this.findViewById(loadingElementIDs[d]); 
     el.setVisibility(LinearLayout.VISIBLE); 
    } 
} 

而這不起作用。如果我從onCreate調用setLoadingStatus(20f);它完美,但不是在AsyncTask中。當然,我確實在活動onCreate()中開始initLoadingBar();new PrefetchData().execute();

你知道我在做什麼錯嗎?

+2

無法從doInBackground()調用UI功能,它是一個單獨的線程(即AsnycTasks的整個目的)。你需要做onProgressUpdate()或onPostExecute() – zgc7009 2014-10-01 21:29:38

回答

0

調用UI功能感謝你們,我onProgressUpdate解決它。而不是setLoadingStatus我稱之爲:

private class PrefetchData extends AsyncTask<Void, Float, Void> { 
    .... 
    protected void onProgressUpdate(Float... values) { 
     for (int d=0; d<Math.min(Math.floor(values[0] * loadingElementIDs.length), loadingElementIDs.length); d++) { 
      LinearLayout el = (LinearLayout)findViewById(loadingElementIDs[d]); 
      el.setVisibility(LinearLayout.VISIBLE); 
     } 
    } 
} 
1

我不知道這是怎麼合併,註釋所有borked,但請求的代碼片段下面是對與處理程序管理這樣的:

在您的活動定義的處理程序:

Handler handler = new Handler(){ 

    handleMessage(Message msg) 
    { 
     if (msg.what == STATUS) 
     { 
      //do something if it's a message form your AsyncTask 
     } 
     else 
     //other messages.. 
    } 

}; 

當創建你的AsyncTask時,給它你的處理程序。定義一個構造函數來接受它,並保存一個本地引用。

new PrefetchData(handler).execute(...); 

,然後你的AsyncTask內:(地位將是一個常數設置爲消息代碼..)

while (...) { 
      step++; 

      // ................... 

      //show status 
      handler.obtainMessage(STATUS, step/totalSteps).sendToTarget(); 
     } 
+0

你有一個例子,我可以如何解決它與處理程序(在AsyncTask內?) – vtni 2014-10-01 21:58:55

0

可以runOnUiThread方法從任何thread

runOnUiThread(new Runnable() { 
    public void run() { 
     // some code #3 (Write your code here to run in UI thread) 
    } 
}); // enter code here 
+0

我不能使用Runnable,因爲我需要'onPostExecute' – vtni 2014-10-01 22:40:03