2016-07-15 55 views
0

我想要使用進度條...但是當我搜索時,進度條不能與Asynctask.get一起使用,但我必須在Asynctask中使用.get和進度。如何使用Asynctask獲取方法進度?

我做了非常簡單的源代碼。 如何更改主線程中顯示進度條?

我想同時使用get方法和UI進度。

public void onCreate(Bundle savedInstanceState) { 
    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AAA asyncTask = new AAA(); 
      try { 
       ((AAA) asyncTask).execute(null, null, null,null).get(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
public class AAA extends AsyncTask<Object, String, Object> { 
    private ProgressDialog progDailog = null; 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progDailog = new ProgressDialog(ViewTestActivity.this); 
     progDailog.setMessage("Loading..."); 
     progDailog.setIndeterminate(false); 
     progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progDailog.setCancelable(true); 
     progDailog.show(); 
    } 

    @Override 
    protected Object doInBackground(Object... params) { 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return params; 
    } 

    @Override 
    protected void onPostExecute(Object result) { 
     progDailog.dismiss(); 
    } 
} 

請幫幫我。 謝謝!

回答

1

調用AsyncTask

private ProgressDialog progDailog = null; 

public void onCreate(Bundle savedInstanceState) { 
    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      AAA asyncTask = new AAA(); 
      try { 
       progDailog = new ProgressDialog(ViewTestActivity.this); 
       progDailog.setMessage("Loading..."); 
       progDailog.setIndeterminate(false); 
       progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
       progDailog.setCancelable(true); 
       progDailog.show(); 
       ((AAA) asyncTask).execute(null, null, null,null); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ExecutionException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 
public class AAA extends AsyncTask<Object, String, Object> { 



    @Override 
    protected Object doInBackground(Object... params) { 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return params; 
    } 

    @Override 
    protected void onPostExecute(Object result) { 
     progDailog.dismiss(); 
    } 
} 
+0

OMG ......感謝非常好!!!!你節省了我的時間! – Adrian

+0

我得到了一個錯誤(?),它在3000秒內運行良好,但是如果我添加10000,它不能正常工作.T_T – Adrian

+0

分享logcat –

0

我不知道是否有一個標準答案之前做,但我只是做了通過設置監聽器在主線程和發送進度消息非常類似的東西來自異步任務的監聽者 - 實際上在我的情況下,它是從數據庫異步加載的。對我來說工作得很好。

+0

ho ??你的意思是..像回調函數? – Adrian

0

你應該更新onProgressUpdate

你的進度條百分比見這個例子 AsyncTask with progress bar

+0

但問題是進度條沒有顯示... – Adrian

+0

我想,你打算說進度對話框,而不是進度條。這就是爲什麼,我誤解了你的問題。 – alian

+0

啊謝謝。我錯過了...進度對話框,謝謝 – Adrian

0

這種嘗試:

private ProgressDialog progDailog; 

public void onCreate(Bundle savedInstanceState) { 
     btn.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
     progDailog = new ProgressDialog(ViewTestActivity.this); 
     progDailog.setMessage("Loading..."); 
     progDailog.setIndeterminate(false); 
     progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progDailog.setCancelable(true); 
     progDailog.show(); 
     AAA asyncTask = new AAA(progDialog); 
     try { 
     ((AAA) asyncTask).execute(null, null, null,null).get(); 
     } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     } 
     }); 
     } 
public class AAA extends AsyncTask<Object, String, Object> { 

    private ProgressDialog progressDialog; 

    public AAA (ProgressDialog progressDialog) { 
     this.progressDialog = progressDialog; 
    } 

    @Override 
    protected Object doInBackground(Object... params) { 
     try { 
      Thread.sleep(3000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return params; 
    } 

    @Override 
    protected void onPostExecute(Object result) { 
     progressDialog.dismiss(); 
    } 
} 
+0

hm ...對不起,如果我延遲睡眠時間,它不會很好.. T_T .. – Adrian