Package totalcross.util.concurrent
Class AsyncTask<Params,Progress,Result>
- java.lang.Object
-
- totalcross.util.concurrent.AsyncTask<Params,Progress,Result>
-
- Type Parameters:
Params-Progress-Result-
public abstract class AsyncTask<Params,Progress,Result> extends java.lang.ObjectAsyncTask provides use of asynchronous operations without ocupying UI Thread with non user interface tasks. In addition, you can easily publish results without the need of manupulating Thread, see the example bellow (Copy and paste this code inside a Container instance):Button dldButton = new Button("download zip"); add(dldButton, CENTER, CENTER); final ProgressBar progressBar = new ProgressBar(); add(progressBar, CENTER, AFTER + UnitsConverter.toPixels(DP + 16), PARENTSIZE + 80, PREFERRED); dldButton.addPressListener((c) -> { new AsyncTask(){ int progress = 0; UpdateListener updateListener = null; @Override protected Object doInBackground(Object... objects) { HttpStream.Options o = new HttpStream.Options(); o.httpType = HttpStream.GET; final String url = " "; if(url.startsWith("https:")) o.socketFactory = new SSLSocketFactory(); try { HttpStream p = new HttpStream(new URI(url)); File f = new File("file.zip", File.CREATE_EMPTY); int totalSize = p.contentLength; byte [] buff = new byte[4096]; BufferedStream bs = new BufferedStream(f, BufferedStream.WRITE, 4096); int counter = 0; while(true) { int size = p.readBytes(buff, 0, buff.length); counter += size; progress = (int)((counter/(double)totalSize)*100); if(size <= 0) break; bs.writeBytes(buff, 0, size); } progress = 100; bs.close(); p.close(); f.close(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { dldButton.setEnabled(false); MainWindow.getMainWindow().addUpdateListener(updateListener = (elapsed) -> { progressBar.setValue(progress); }); } @Override protected void onPostExecute(Object result) { dldButton.setEnabled(true); MainWindow.getMainWindow().removeUpdateListener(updateListener); } }.execute(); }); - Since:
- TotalCross 4.3.9
-
-
Constructor Summary
Constructors Constructor Description AsyncTask()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract ResultdoInBackground(Params... params)Method in background thread when execute method is calledAsyncTask<Params,Progress,Result>execute(Params... params)executedoInBackground(java.lang.Object[])outside UI Threadprotected voidonPostExecute(Result result)Method called in UI Thread when the task executed indoInBackground(java.lang.Object[])is finished.protected voidonPreExecute()Process task on pre execute in the thread in which this method was calledprotected voidonProgressUpdate(Progress... values)Override this method if you want to Provide progress to the UI Thread when calling publishProgress inside doInBackground.protected voidpublishProgress(Progress... values)CallsonProgressUpdate(java.lang.Object[])in UI Thread
-
-
-
Method Detail
-
onPreExecute
protected void onPreExecute()
Process task on pre execute in the thread in which this method was called
-
doInBackground
protected abstract Result doInBackground(Params... params)
Method in background thread when execute method is called- Parameters:
params-- Returns:
- See Also:
execute(java.lang.Object[])
-
onProgressUpdate
protected void onProgressUpdate(Progress... values)
Override this method if you want to Provide progress to the UI Thread when calling publishProgress inside doInBackground.- Parameters:
values-- See Also:
doInBackground(java.lang.Object[]),publishProgress(java.lang.Object[])
-
publishProgress
@SafeVarargs protected final void publishProgress(Progress... values)
CallsonProgressUpdate(java.lang.Object[])in UI Thread- Parameters:
values-- See Also:
onProgressUpdate(java.lang.Object[])
-
onPostExecute
protected void onPostExecute(Result result)
Method called in UI Thread when the task executed indoInBackground(java.lang.Object[])is finished.- Parameters:
result-- See Also:
doInBackground(java.lang.Object[])
-
-