package com.example.day9;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private Button bt1;
private ProgressDialog dialog;
private String url="http://b.hiphotos.baidu.com/image/w%3D2048/sign=0240c37eb0119313c743f8b051000dd7/e4dde71190ef76c6f6ecf6979f16fdfaaf51674b.jpg";
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView=(ImageView)super.findViewById(R.id.iv);
this.dialog=new ProgressDialog(this);
this.dialog.setTitle("提示框");
this.dialog.setMessage("正在下载图片..");
this.bt1=(Button)super.findViewById(R.id.bt1);
this.bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new ImageDowload().execute(url);
}
}); }
public class ImageDowload extends AsyncTask<String, Void, Bitmap>{
//第一个参数为任务,这里指的是下载链接
//第二个参数是进度,一般是Integer
//第三个参数是执行结果返回类型,这里是图片
public Bitmap doInBackground(String... arg0) {
// 耗时操作在这里执行,参数为任务类型,这里是指下载链接(String)
HttpClient client=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(arg0[0]);
Bitmap bitmap=null;
Log.i("lcw", arg0[0]);
try {
HttpResponse httpResponse = client.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
Log.i("www", httpResponse+"");
HttpEntity entity=httpResponse.getEntity();
byte data[]=EntityUtils.toByteArray(entity);
bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return bitmap;
} @Override
public void onPostExecute(Bitmap result) {
// 最后操作,更新UI操作
super.onPostExecute(result);
Log.i("11111111", "eeee");
MainActivity.this.imageView.setImageBitmap(result);
dialog.dismiss();
} @Override
public void onPreExecute() {
// 最早操作,预处理动作
super.onPreExecute();
MainActivity.this.dialog.show();
} } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.day9.MainActivity"
tools:ignore="MergeRootFrame" > <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <Button
android:id="@+id/bt1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="点击下载图片"
/> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day9"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.day9.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

效果图如下:

AsyncTask异步类的简单操作的更多相关文章

  1. Android中的AsyncTask异步任务的简单实例

    在 Android中的AsyncTask异步任务的简介 一文中.已经对 安卓 异步任务操作做了简单的介绍.这里,直接将上文中的异步任务做了一个实例.实现异步操作更新UI线程,相比开启子线程更新来说逻辑 ...

  2. 【转】c# 类反射简单操作

    转:http://www.jb51.net/article/25863.htm 首先建立一个测试的类  复制代码代码如下: public class MyClass { public int one ...

  3. Android中异步类AsyncTask的理解

    这里有两种解释的方法,各有侧重点: 第一种解释: Async Task 简介:AsyncTask的特点是任务在主线程之外运行,而回调方法是在主线程中执行,这就有效地避免了使用Handler带来的麻烦  ...

  4. C#反射技术的简单操作(读取和设置类的属性)

    public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type ...

  5. 使用AsyncTask异步更新UI界面及原理分析

    概述: AsyncTask是在Android SDK 1.5之后推出的一个方便编写后台线程与UI线程交互的辅助类.AsyncTask的内部实现是一个线程池,所有提交的异步任务都会在这个线程池中的工作线 ...

  6. Android中AsyncTask异步

    今天我们学习了 AsyncTack, 这是一个异步任务. 那么这个异步任务可以干什么呢? 因为只有UI线程,即主线程可以对控件进行更新操作.好处是保证UI稳定性,避免多线程对UI同时操作. 同时要把耗 ...

  7. Android中使用Thread线程与AsyncTask异步任务的区别

    最近和几个朋友交流Android开发中的网络下载问题时,谈到了用Thread开启下载线程时会产生的Bug,其实直接用子线程开启下载任务的确是很Low的做法,那么原因究竟如何,而比较高大上的做法是怎样? ...

  8. Android线程管理之AsyncTask异步任务

    前言: 前面几篇文章主要学习了线程以及线程池的创建与使用,今天来学习一下AsyncTask异步任务,学习下AsyncTask到底解决了什么问题?然而它有什么弊端?正所谓知己知彼百战百胜嘛! 线程管理相 ...

  9. 【转】【C#】C# 5.0 新特性——Async和Await使异步编程更简单

    一.引言 在之前的C#基础知识系列文章中只介绍了从C#1.0到C#4.0中主要的特性,然而.NET 4.5 的推出,对于C#又有了新特性的增加--就是C#5.0中async和await两个关键字,这两 ...

随机推荐

  1. C#学习笔记(16)——C#中重写(override)和覆盖(new)的区别

    说明(2017-7-17 23:04:45): 原文: C#中重写(override)和覆盖(new)的区别 重写 用关键字 virtual 修饰的方法,叫虚方法.可以在子类中用override 声明 ...

  2. Drozer快速使用指南

    1.简介: Drozer是一款用于测试android应用程序漏洞的安全评估工具,能够发现多种类型的安全的漏洞,免费版本的相关资源下载地址: https://www.mwrinfosecurity.co ...

  3. mysql 乱码解决方案

    如何解决MYSQL数据中文乱码问题? 第一种方法,总结: 经常更换虚拟主机,而各个服务商的MYSQL版本不同,当导入数据后,总会出现乱码等无法正常显示的问题,查了好多资料,总结出自己的一点技巧: WI ...

  4. 【微信小程序】tabBar的显示问题

    tabBar不显示 在app.json中配置了4个页面,在tabBar的list中随意写了两个页面,编译后发现不能显示tabBar. { "pages": [ "page ...

  5. busybox tar 命令支持 tar.gz

    原始的 busybox 里面的 tar 命令不支持 tar.gz 解压 在 busybox-menuconfig 里面加入 下面的选项即可

  6. session过期跳出irame

    在登录页底部加入 <script type="text/javascript"> if (window != top) top.location.href = loca ...

  7. Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth

    1. 概述 Spring Cloud Sleuth实现对Spring cloud 分布式链路监控 本文介绍了和Sleuth相关的内容,主要内容如下: Spring Cloud Sleuth中的重要术语 ...

  8. Spring Cloud Config 自动刷新所有节点 架构改造

    详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p162-9.9.4节 要做的改动是: 1.在spring cloud config server 服务端加入 spr ...

  9. PCL点云曲面重建(1)

    在测量较小的数据时会产生一些误差,这些误差所造成的不规则数据如果直接拿来曲面重建的话,会使得重建的曲面不光滑或者有漏洞,可以采用对数据重采样来解决这样问题,通过对周围的数据点进行高阶多项式插值来重建表 ...

  10. eclipse中设置字体为VC经典字体Fixedsys