AsyncTask异步任务执行

1.添加网络授权


2.UI的主线程中不能直接访问网络
3.异步任务操作

public class MainActivity extends AppCompatActivity {
    private Button button;
    private ImageView imageView;
    private String image_path = "";
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = this.findViewById(R.id.button1);
        imageView = this.findViewById(R.id.imageView1);
        dialog = new ProgressDialog(this);
        dialog.setTitle("提示信息");
        dialog.setMessage("正在下载图片");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyTask().execute(image_path);
            }
        });
    }

    /**
     * 路径、刻度、返回值类型
     */
    public class MyTask extends AsyncTask {

        // 执行任务之前的操作
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        // 完成耗时操作
        @Override
        protected Bitmap doInBackground(String... params) {
            return null;
        }

        //更新UI操作
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
        }
    }
}

你可能感兴趣的:(AsyncTask异步任务执行)