运行Android后台操作时显示加载对话框

时间:2013-01-21 10:04:59

标签: android background android-asynctask loading

我需要在android上做一些涉及逐像素编辑的图像处理。这需要一点时间,屏幕只是冻结在那里,所以我想做一个加载对话框,让用户知道程序仍在运行。

这个editPhoto功能是我需要在后台运行的。

 private void editPhoto() {
    if (editPhotoImg.getDrawable() == null) {
        Toast.makeText(getApplicationContext(), "No Photo Selected",
                Toast.LENGTH_SHORT).show();
    } else if (hasSample == false) {
        Toast.makeText(getApplicationContext(), "No Sample Photo Selected",
                Toast.LENGTH_SHORT).show();
    } else {


        detectFaces(false);


        BitmapDrawable ebm = (BitmapDrawable) editPhotoImg.getDrawable();

        Bitmap editTemp = ebm.getBitmap();
        PointF editFaceMidPoint = getFaceMidPoint(editTemp);

        BitmapDrawable sbm = (BitmapDrawable) samplePhotoImg.getDrawable();
        Bitmap sampleTemp = sbm.getBitmap();
        PointF sampleFaceMidPoint = getFaceMidPoint(sampleTemp);

        if (editFaceMidPoint != null && sampleFaceMidPoint != null) {
            int editFaceMidPointPixel = editTemp.getPixel(
                    (int) editFaceMidPoint.x, (int) editFaceMidPoint.y);
            int sampleFaceMidPointPixel = sampleTemp.getPixel(
                    (int) sampleFaceMidPoint.x, (int) sampleFaceMidPoint.y);

            editPhotoImg.setImageBitmap(shiftRGB(editTemp,
                    editFaceMidPointPixel, sampleFaceMidPointPixel));

            savePhoto();

            detectFaces(true);
        }
    }

}

这是我的AsyncTask代码,它无法达到我想要的效果。它给了我一个我甚至无法识别的错误。有人可以告诉我该怎么办,或者我应该在哪里调用editPhoto()方法,以便它在加载对话框后面而不是在加载对话框之前或之后运行?

     private class LoadEditPhoto extends AsyncTask<Void, Integer, Void> {
    // Before running code in separate thread

    @Override
    protected void onPreExecute() {
        /* Create a new progress dialog
        progressDialog = new ProgressDialog(EditPhoto.this);
        // Set the progress dialog to display a horizontal progress bar
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // Set the dialog title to 'Loading...'
        progressDialog.setTitle("Loading...");
        // Set the dialog message to 'Loading application View, please
        // wait...'
        progressDialog.setMessage("Loading application View, please wait...");
        */
        progressDialog = ProgressDialog.show(EditPhoto.this,"Loading...",  
                "Editing Photo, please wait...", false, false);  
        // This dialog can't be canceled by pressing the back key
        progressDialog.setCancelable(false);
        // This dialog isn't indeterminate
        progressDialog.setIndeterminate(false);
        // The maximum number of items is 100
        progressDialog.setMax(100);
        // Set the current progress to zero
        progressDialog.setProgress(0);
        // Display the progress dialog
        progressDialog.show();
    }

    // The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {
        /*
         * This is just a code that delays the thread execution 4 times,
         * during 850 milliseconds and updates the current progress. This is
         * where the code that is going to be executed on a background
         * thread must be placed.
         * 
         */


        editPhoto();
        publishProgress(100);
        return null;
    }

    // Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        // set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) {
        // close the progress dialog
        progressDialog.dismiss();

    }
}

2 个答案:

答案 0 :(得分:1)

删除toast消息,它应该工作。您无法从doInBackground方法更新UI。除此之外你的代码看起来很好。所以这应该是bug。

或使用runOnUi()显示Toast消息。

像这样围着你的吐司,

ActivityObject.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        }
    });

答案 1 :(得分:1)

editPhoto()放入主题

现在定义以下代码来处理多条消息。

public enum WhatAbout {
    START,STOP
}

public WhatAbout[] wa = WhatAbout.values();

然后使用Handler,这有助于您与BackgroundThreadUIThread进行沟通。 如果不使用处理程序,则无法更改UI

将以下代码放入onCreate方法。

handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    if (msg.what < wa.length) {
    switch (wa[msg.what]) 
    {
    case START:
         progressDialog = ProgressDialog.show(getActivity(), null,
                        "Editing Photo... Please Wait...");
         break;

    case STOP:
         if (progressDialog.isShowing())
            progressDialog.dismiss();

         imageView.setImageBitmap(editedBitmap);
         break;
    }
  }
};

然后当Thread中的 编辑开始 时,请将以下代码放在editPhoto()的第一行。

handler.sendMessage(handler.obtainMessage(
                WhatAbout.START.ordinal(), 0, 0));

完成editing后,请在editPhoto()末尾放置以下代码。

handler.sendMessage(handler.obtainMessage(
                WhatAbout.STOP.ordinal(), 0, 0));