Android AsyncTask异步任务

AsyncTask< 地址类型Params, 进度值类型Progress, 结果类型Result> {    //String、URL, Integer, Bitmap、void

        onPreExecute(){    //运行在主线程MainThread,准备工作 }

                totalLen = connection.getContentLength();

        protect Result doInBackground(Params... params) {        //运行在子线程WorkThread,耗时操作        return result;}

                int progress = (int) (curLen*100/(double)totalLen);

                publishProgress(progress);

        onPostExcute(Result result) {        //在主线程中调用,处理结果  }

                BitmapFactory.decodeByteArray(bytes, 0, bytes.length);        把一个字节数组变成Bitmap对象

        onProgressUpdate(Progress... progress) {        //在publishProgress()调用之后在主线程调用,用来显示进度值 }

                ProgressDialog dialog = new ProgressDialog(this);

                dialog.setTitle("...");

                dialog.setProgressStyled(ProgressDialog.STYLE_HORIZONTAL);

                dialog.setIcon(...);

                dialog.setMessage("Loading...");

                dialog.setCanceledOnTouchOutside(false);

                dialog.show();

                dialog.setProgress(int value);

                dialog.setOnDismissListener(

                      onDismiss(DialogInterface dialog) {       //myAsync.cancel(true);    }

                )

                dialog.dismiss();

        onCanceled(Result result) {        //取消异步任务之后再主线程中运行}

}

主线程:new MyAsyncTask.excute(path);//一个MyAsyncTask对象只能调用excute()一次

 

转载于:https://my.oschina.net/glfei/blog/3007857

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值