关于java:安卓-AsyncTask-异步任务

50次阅读

共计 2614 个字符,预计需要花费 7 分钟才能阅读完成。

package com.kk.laodi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AsyncTask1 extends AppCompatActivity {

private Button btn_dl, right_hand, left_hand, right_foot, left_foot;
private ImageView img01;
private String image_path = "https://img.lianzhixiu.com/uploads/allimg/202104/9999/7490b1acfa.jpg";
private ProgressDialog dialog;
private MyTask myTask;  // 异步工作写里面,不要创立实例。@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_task1);
    btn_dl = (Button)this.findViewById(R.id.btn_dl);
    right_hand = (Button)this.findViewById(R.id.right_hand);
    right_foot = (Button)this.findViewById(R.id.right_foot);
    left_hand = (Button)this.findViewById(R.id.left_hand);
    left_foot = (Button)this.findViewById(R.id.left_foot) ;
    img01 = (ImageView)this.findViewById(R.id.img01);
    dialog = new ProgressDialog(this);
    dialog.setTitle("提醒");
    dialog.setMessage("正在下载,[PayPal 下载](https://www.gendan5.com/wallet/PayPal.html) 请稍后...");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  // 对话框外面设置进度条。最初显示对话框就能够。

// dialog.setCancelable(false); // 设置不能隐没

    btn_dl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {if(myTask != null && !myTask.isCancelled()) {return;}
            myTask = new MyTask();
            myTask.execute(image_path);
        }
    });
    left_hand.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {if(myTask !=null && myTask.getStatus() == AsyncTask.Status.RUNNING){myTask.cancel(true);
            }
        }
    });
}
public class MyTask extends AsyncTask<String, Integer, Bitmap>{
    @Override
    protected void onPreExecute() {super.onPreExecute();

// dialog.show();

    }
    @Override
    protected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);

// dialog.setProgress(values[0]);

        if(isCancelled()){return;}
        switch (values[0]) {
            case 1:
                left_foot.setText("点我");
                break;
            case 2:
                left_hand.setText("点我");
                break;
            case 3:
                right_foot.setText("点我");
                break;
            case 4:
                right_hand.setText("点我");
                break;
            case 5:
                left_foot.setText("左脚");
                break;
            case 6:
                left_hand.setText("左手");
                break;
            case 7:
                right_foot.setText("右脚");
                break;
            case 8:
                right_hand.setText("右手");
                break;
        }
    }
    @Override
    protected Bitmap doInBackground(String... params) {
        /**
         * 实现闪按钮性能
         */
        int i = 0;
        while(i <= 100){if(isCancelled()){break;}
            publishProgress(i);
            i = i % 8;
            delay_ms(2000);
            publishProgress(i);
            i++;
        }

//

        return null;
    }
    @Override
    protected void onPostExecute(Bitmap result) {super.onPostExecute(result);
        dialog.dismiss();
        img01.setImageBitmap(result);
    }
}
public void delay_ms(int t) {
    try
    {Thread.currentThread().sleep(t);// 毫秒
    }
    catch(Exception e){}}

}

正文完
 0