获取线程导致 Android 的另一个 activity

Getting thread result in another activity for Android

My Main Activity 调用 ResultActivity 并同时调用 Runnable 线程。我希望结果 activity 不等待结果处理的某些部分(图像),这可能稍后由线程提供,并且 UI 可能会相应更新。

下面给出了我尝试做的事情。

MainActivity(下面的方法实际上是来自另一个threadcallback):

@Override
public void onCreate(Context context) {
    resultImageProcessor = new ResultImageProcessor();
    resImgProThread = new Thread(resultImageProcessor);
}

@Override
public void onBarcodeDetected(final Barcode barcode) {
    resultImageProcessor.setCameraBarcode(mCameraSource,barcode);
    resImgProThread.start();

    Intent intent = new Intent(this, ResultActivity.class);
    intent.putExtra(BarcodeObject, barcode);
    intent.putExtra(ResultCode, CommonStatusCodes.SUCCESS);
    intent.putExtra(ResImgProcObject, resultImageProcessor);
    startActivity(intent);
}

结果图像处理器:

public class ResultImageProcessor implements Serializable, Runnable {

    private ResultActivity resultActivityContext;

    ResultImageProcessor(){
        this.resultActivityContext = null;
    }

    public void setResultActivity(ResultActivity resultActivity) {
        this.resultActivityContext = resultActivity;
    }

    public void setCameraBarcode(CameraSource cameraSource, Barcode barCode){
        mCameraSource = cameraSource;
        barcode = barCode;
    }

    @Override
    public void run() {
        String picPath = ProcessImage(Obj..Attributes);
        //wait until result activity context is not set
        while(resultActivityContext == null){
            try {
                sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        resultActivityContext.onImageSaved(picPath);
    }
}

结果Activity:

protected void onCreate(Bundle savedInstanceState) {
    //...
    data = getIntent();
    Barcode barcode = data.getParcelableExtra(MainActivity.BarcodeObject);
    ResultImageProcessor resultImageProcessor = data.getParcelableExtra(MainActivity.ResImgProcObject);
    resultImageProcessor.setResultActivity(this);
}

//called from Result image processor
public void onImageSaved(String imagePath){
    ImageView barImgView = findViewById(R.id.barcode_image);
    Bitmap barcodeImage = BitmapFactory.decodeFile(imagePath);
    barImgView.setImageBitmap(barcodeImage);
    barImgView.invalidate();
}

使用上面的代码,在调用resultImageProcessor.startProcessing()之后,结果activity没有启动,runnable的run()方法在while循环中保持忙碌。我使用记录器追踪它们。当我跳过线程并将图像路径传递给 activity 时,除了 activity 切换速度变慢之外,一切都很好。

请指出问题或建议更好的解决方案。

我认为你在 运行 中缺少 运行 像这样

new Runnable() {
            @Override
            public void run() {
                String picPath = ProcessImage(Obj..Attributes);
                //wait until result activity context is not set
                while(resultActivityContext == null){}
                resultActivityContext.onImageSaved(picPath);
            }
        }.run();

事实证明,问题在于将 ResultImageProcessor 对象作为 Parcelable 传递给 ResultActivity 意图。我遵循了在 ResultImageProcessor.

中将 resultActivityContext 声明为静态的简单路径
public class ResultImageProcessor implements Runnable {
    public static ResultActivity resultActivityContext;
    ...
    @Override
    public void run() {
        ...
        resultActivityContext.onImageSaved(picPath);
        resultActivityContext = null;
    }
}

在 ResultActivity 中:

ResultImageProcessor.resultActivityContext = this;