官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 刚察县| 宁安市| 连江县| 五常市| 浏阳市| 呈贡县| 楚雄市| 蓬莱市| 孝感市| 永新县| 睢宁县| 安平县| 壶关县| 凯里市| 无棣县| 新沂市| 民和| 西丰县| 响水县| 陵川县| 武宣县| 无锡市| 梓潼县| 泗洪县| 巴中市| 迁安市| 浠水县| 红河县| 宣化县| 闵行区| 马尔康县| 承德市| 高邮市| 碌曲县| 高邑县| 淮安市| 明溪县| 砚山县| 金秀| 玛纳斯县| 屏东市|