在Activity中獲取AsyncTask中更新progress的值,可用在檔案的上傳和下載同步顯示進度
public class MyAsyncTask extends AsyncTask<String, Integer, Void>{
public AsyncResponse mAsyncResponse = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params){
//執行緒體,需要訪問網路的一些操作
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// 在Activity'中獲取progress更新的值
if(mAsyncResponse != null){
mAsyncResponse.progressUpdate(values[0]);
}
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
//建立一個介面
public interface AsyncResponse{
public void progressUpdate(int value);
}
}
//在Activity中
public class MyActivity extends Activity implements AsyncResponse{
private MyAsyncTask task;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(//佈局檔案...);
task = new MyAsyncTask();
task.mAsyncResponse
}
@Override
public void progressUpdate(int value) {
// TODO Auto-generated method stub
//進行一些介面UI中控制元件值的更新
}
}