如何将 AsyncTask 更改为 @background 和 @UiThread

how to change AsyncTask to @background and @UiThread

谁能帮我把 AsyncTask 改成@Background 和@UiThread? 我应该如何开始?我应该删除什么? onPost 和 onPre 应该转换为 UiThread? 我将非常感谢您的建议

这是我的一段代码。

@Click
    void button1(View view)
    {

        ReadPic rp = new ReadPic();
        rp.execute();
    }

    class ReadPic extends AsyncTask<Void, Void, Void>
    {
        ProgressBar progressBar;
        boolean sukces = true;

        public ReadPic()
        {
            progressBar = new ProgressBar(getBaseContext());

        }

        @Override
        protected Void doInBackground(Void... arg0)
        {
            URL u;
            InputStream is;

            try
            {
                u = new URL( adres);
                is = u.openStream();
                Bitmap temp = BitmapFactory.decodeStream(is);
                bmp=temp.copy(Bitmap.Config.ARGB_8888, true);
            }
            catch( Exception e)
            {
                sukces = false;

            }
            return null;
        }
        @Override
        protected void onPreExecute()
        {
            if(progressBar !=null)
                progressBar.setVisibility(ProgressBar.VISIBLE);


            button1.setEnabled(false);
            if(progressBar !=null)
                progressBar.setVisibility(ProgressBar.INVISIBLE);
            textView1.setText("DOWNLOADING...");
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(Void result)
        {
            if(sukces)
            {


                imageView.setImageBitmap(bmp);
                imageView1.setImageBitmap(bmp);
                imageView2.setImageBitmap(bmp);
                imageView3.setImageBitmap(bmp);
                imageView4.setImageBitmap(bmp);
                imageView5.setImageBitmap(bmp);
                imageView6.setImageBitmap(bmp);
                imageView7.setImageBitmap(bmp);
                imageView8.setImageBitmap(bmp);

                textView1.setText("Downloading finished");
            }
            else
            {
                textView1.setText("Error when downloading");
            }
            button1.setEnabled(true);
            progressBar.setVisibility(ProgressBar.INVISIBLE);
            super.onPostExecute(result);
        }

你可以这样转换:

@Click
void button1(View view) {
  startProcess();
}

void startProcess() {
  if(progressBar !=null) // progressBar should be a field in your Activity / Fragment
      progressBar.setVisibility(ProgressBar.VISIBLE);


  button1.setEnabled(false);
  if(progressBar !=null)
      progressBar.setVisibility(ProgressBar.INVISIBLE);
  textView1.setText("DOWNLOADING...");

  yourBackgroundMethod();
}

@Background
void yourBackgroundMethod() {
  URL u;
  InputStream is;

  try
  {
      u = new URL( adres);
      is = u.openStream();
      Bitmap temp = BitmapFactory.decodeStream(is);
      bmp=temp.copy(Bitmap.Config.ARGB_8888, true);

      onBackgroundSuccess();
      onBackgroundTerminate();
  }
  catch( Exception e)
  {
      onBackgroundError();
      onBackgroundTerminate();
  }
}

@UiThread
void onBackgroundError() {
  imageView.setImageBitmap(bmp);
  imageView1.setImageBitmap(bmp);
  imageView2.setImageBitmap(bmp);
  imageView3.setImageBitmap(bmp);
  imageView4.setImageBitmap(bmp);
  imageView5.setImageBitmap(bmp);
  imageView6.setImageBitmap(bmp);
  imageView7.setImageBitmap(bmp);
  imageView8.setImageBitmap(bmp);

  textView1.setText("Downloading finished");
}

@UiThread
void onBackgroundSuccess() {
  textView1.setText("Error when downloading");
}

@UiThread
void onBackgroundTerminate() {
  button1.setEnabled(true);
  progressBar.setVisibility(ProgressBar.INVISIBLE);
}