- Asynchronous Android Programming(Second Edition)
- Helder Vasconcelos
- 398字
- 2021-07-14 10:43:15
Providing indeterministic progress feedback
Having started what we know to be a potentially long-running task, we probably want to let the user know that something is happening. There are a lot of ways of doing this, but a common approach is to present a dialog displaying a relevant message.
A good place to present our dialog is from the onPreExecute()
method of AsyncTask
which executes on the main thread so it is allowed to interact with the user interface.
The modified DownloadImageTask
will need a reference to a Context, so that it can prepare a ProgressDialog
, which it will show and dismiss in onPreExecute()
and onPostExecute()
respectively. As doInBackground()
has not changed, it is not shown in the following code, for brevity:
public class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> { ... private final WeakReference<Context> ctx; private ProgressDialog progress; ... public DownloadImageTask(Context ctx, ImageView imageView) { this.imageView = new WeakReference<ImageView>(imageView); this.ctx = new WeakReference<Context>(ctx); } @Override protected void onPreExecute() { if ( ctx !=null && ctx.get()!= null ) { progress = new ProgressDialog(ctx.get()); progress.setTitle(R.string.downloading_image); progress.setIndeterminate(true); progress.setCancelable(false); progress.show(); } } // ... doInBackground elided for brevity ... @Override protected void onPostExecute(Bitmap bitmap) { ... if ( progress != null ) { progress.dismiss(); } ... } }
All that remains is to pass a Context
to the constructor of our modified DownloadImageTask
. As Activity
is a subclass of Context
, we can simply pass a reference to the host Activity
:
showBut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ... // Pass in the Context and the image view to load // the image new DownloadImageTask( ShowMyPuppyActivity.this, iv).execute(url); ... } });

Figure 3.3 : Indeterministic Progress Dialog
Once the async task is started, the onPreExecute()
callback will create an indeterministic progress dialog and display it as shown in Figure 3.3. The non-cancelable dialog will be placed over the UI screen in an opaque layer with the title defined. By indeterministic, we mean that beforehand, we can't estimate how much longer we have to wait for the task to complete.
Until the download finishes, and the dialog gets dismissed on onPostExecute()
, the user is not able to interact with the application and the dialog will remain in the foreground.
Note
When any long computation is required before you are able to present your content in your application UI, you must present an indication that something is happening in the background while the user is waiting.
- DBA攻堅指南:左手Oracle,右手MySQL
- Spring 5.0 By Example
- TensorFlow Lite移動端深度學習
- Beginning Java Data Structures and Algorithms
- 微服務與事件驅(qū)動架構
- Learning Informatica PowerCenter 10.x(Second Edition)
- Learning Linux Binary Analysis
- Lua程序設計(第4版)
- Building Minecraft Server Modifications
- Python編程與幾何圖形
- Python數(shù)據(jù)結構與算法(視頻教學版)
- HTML5從入門到精通(第4版)
- 小程序,巧應用:微信小程序開發(fā)實戰(zhàn)(第2版)
- QGIS Python Programming Cookbook(Second Edition)
- Go語言開發(fā)實戰(zhàn)(慕課版)