AsyncTask再一次

时间:2012-06-29 12:57:28

标签: android android-asynctask

完成后是否有办法运行我的AsyncTask?

我的应用是捕获图像并将其放在imagView上,然后在其上执行AsyncTask。 我有另一个按钮拍摄另一张照片,然后把它放在不能执行AysncTask。

我知道表单developer.Android,AsyncTask除了一次之外不能执行。

这是我的代码。

package com.ocr.id;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.ocr.id.ip.AndroidImage;
import com.ocr.id.ip.Binarization;
import com.ocr.id.ip.Crop;
import com.ocr.id.ip.Segement;

public class PreviewActivity extends Activity {

    private ImageView previewIV;
    private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private String path;
    private boolean crop = true;
    private boolean resample = true;
    Crop cropID;
    Binarization binary;
    Segement seg;
    ProgressDialog previewPD;

    OnClickListener processOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            segementTask task = new segementTask();

            task.execute(path);
            }
    };

    private OnClickListener backOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            startActivityForResult(
                    new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
                            MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(path))),
                    CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                previewIV.setImageBitmap(AndroidImage
                        .decodeSampledBitmapFromSDcard(path, 1000, 1000));
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview);

        path = getIntent().getExtras().getString("path");

        previewIV = (ImageView) findViewById(R.id.previewPicID);

        Button process = (Button) findViewById(R.id.processID);
        process.setOnClickListener(processOnClickListener);

        Button back = (Button) findViewById(R.id.back);
        back.setOnClickListener(backOnClickListener);

        previewIV.setImageBitmap(AndroidImage.decodeSampledBitmapFromSDcard(
                path, 1000, 1000));
    }

    class segementTask extends AsyncTask<String, Integer, Void> {

        @Override
        protected void onPreExecute() {
            previewPD = ProgressDialog.show(PreviewActivity.this, "Id-OCR",
                    "Processing...");
            previewPD.setCancelable(false);
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                seg = new Segement(PreviewActivity.this, params[0]);
                seg.segmentNumbers();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            previewPD.dismiss();
            previewPD = null;

            Toast.makeText(PreviewActivity.this, "Processing done",
                    Toast.LENGTH_LONG).show();
            super.onPostExecute(result);
        }
    }
}

5 个答案:

答案 0 :(得分:2)

您无法两次运行同一个实例。但您可以根据需要多次拨打new segmentTask().execute()

答案 1 :(得分:1)

如果要保留相同的工作线程,并在第一个图像完成后立即开始处理第二个图像,则更好的解决方案可能是使用Handler。它允许您发布作业,以便按顺序执行。

答案 2 :(得分:0)

设定点数

int mNumTaskFinished = 0;

创建受保护的方法:

protected method didTaskFinished( AsyncTask task ){
     if(mNumTaskFinished == 0){
         task.execute(path);
     }
}

在您的AsyncTask中,您可以调用:

@Override
    protected void onPostExecute(Void result) {
        previewPD.dismiss();
        previewPD = null;

        Toast.makeText(PreviewActivity.this, "Processing done",
                Toast.LENGTH_LONG).show();

        didTaskFinished(this);

        super.onPostExecute(result);
    }

答案 3 :(得分:0)

protected void onPostExecute(Void result) {
            previewPD.dismiss();
            previewPD = null;

            Toast.makeText(PreviewActivity.this, "Processing done",
                    Toast.LENGTH_LONG).show();
            super.onPostExecute(result);'

            if(setAcondition) {
                new segementTask().execute(whateverPath);
            }

        }

如果您正在寻找,请告诉我。

答案 4 :(得分:0)

尝试:

    @Override
    protected void onPostExecute(Void result) {
        previewPD.dismiss();
        previewPD = null;


        if (task.getStatus() == AsyncTask.Status.FINISHED)
            tTaskFinished();

        Toast.makeText(PreviewActivity.this, "Processing done",
                Toast.LENGTH_LONG).show();
        super.onPostExecute(result);
    }

protected void tTaskFinished( ){

     task = new MyAsyncTask();
     task.execute(path);
}