package com.cnn.asynctask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
private Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnButton=(Button) findViewById(R.id.button1);
btnButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
Intent intent = new Intent(MainActivity.this, ImageTest.class);
startActivity(intent);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

  

package com.cnn.asynctask;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar; public class ImageTest extends Activity {
private ImageView imageView;
private ProgressBar bar;
private String urlString="http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.image); imageView=(ImageView) findViewById(R.id.image);
bar=(ProgressBar) findViewById(R.id.bar); new ImageAsync().execute(urlString);
} class ImageAsync extends AsyncTask<String, Void, Bitmap> { @Override
protected Bitmap doInBackground(String... params) {
// TODO 自动生成的方法存根
//获取传递进来的参数
String urlString=params[0];
Bitmap bitmap=null;
URLConnection connection;
InputStream inputStream;
try {
connection=new URL(urlString).openConnection();
inputStream=connection.getInputStream();
BufferedInputStream bisBufferedInputStream= new BufferedInputStream(inputStream);
//通过decodeStream方法解析输入流 转化为bitmap
bitmap=BitmapFactory.decodeStream(bisBufferedInputStream);
inputStream.close();
bisBufferedInputStream.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//bitmap作为返回值返回给后面调用的方法
return bitmap;
} @Override
protected void onPreExecute() {
// TODO 自动生成的方法存根
super.onPreExecute();
bar.setVisibility(View.VISIBLE);
} @Override
protected void onPostExecute(Bitmap result) {
// TODO 自动生成的方法存根
super.onPostExecute(result);
bar.setVisibility(View.GONE);
imageView.setImageBitmap(result);
}
}
}

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cnn.asynctask.MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" /> </RelativeLayout>

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="@+id/bar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cnn.asynctask"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<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=".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> <activity
android:name=".ImageTest"
android:label="@string/app_name" >
</activity>
</application> </manifest>

  

安卓开发--AsyncTask的更多相关文章

  1. 安卓开发30:AsyncTask的用法

    http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43)  在开发Android应用时必须遵守单线程模型的原则: Andro ...

  2. 安卓开发笔记——关于AsyncTask的使用

    在安卓开发中,我们经常要进行一些耗时操作,比如数据库操作,获取网络资源,读取内存文件等等,当我们在处理这些耗时操作的时候,如果我们直接在UI主线程进行,那么可能会导致阻塞UI主线程,使得UI界面卡顿, ...

  3. 安卓开发笔记——关于照片墙的实现(完美缓存策略LruCache+DiskLruCache)

    这几天一直研究在安卓开发中图片应该如何处理,在网上翻了好多资料,这里做点小总结,如果朋友们有更好的解决方案,可以留言一起交流下. 内存缓存技术 在我们开发程序中要在界面上加载一张图片是件非常容易的事情 ...

  4. 学习安卓开发[5] - HTTP、后台任务以及与UI线程的交互

    在上一篇学习安卓开发[4] - 使用隐式Intent启动短信.联系人.相机应用中了解了在调用其它应用的功能时隐式Intent的使用,本次基于一个图片浏览APP的开发,记录使用AsyncTask在后台执 ...

  5. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

  6. 安卓中AsyncTask的基本使用

    安卓中AsyncTask的基本使用 使用场景介绍 在安卓开发中,我们经常需要访问互联网资源,这些访问是都需要在后台线程中去完成的,因为安卓的UI线程不允许执行耗时任务.然而,后台线程是不可以修改安卓的 ...

  7. 基于eclipse-java的平台上搭建安卓开发环境

    首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...

  8. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  9. Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境

    我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...

随机推荐

  1. linux 命令 xxd

    xxd,能够查看linux下文件的二进制表示.man一下xxd.能够得到下面信息 NAME        xxd - make a hexdump or do the reverse. SYNOPSI ...

  2. 3、Android中Activity的跳转

    1.创建project         file->new->android application 依次填入应用名称.project名.包名 在project文件夹下找到src/com. ...

  3. 一分钟了解Android横竖屏 mdpi hdpi xhdpi xxhdpi xxxhdpi

    DPI:每英寸像素数 简单的屏幕分辨率计算方法: DisplayMetrics metrics = this.getResources().getDisplayMetrics(); float den ...

  4. Java多线程之~~~线程安全容器的非堵塞容器

    在并发编程中,会常常遇到使用容器.可是假设一个容器不是线程安全的.那么他在多线程的插入或者删除的过程 中就会出现各种问题.就是不同步的问题.所以JDK提供了线程安全的容器,他能保证容器在多线程的情况下 ...

  5. cocos2d-x 2.2.2 在lua中更换CCSprite的图片

    废话不多说,直接上代码 --lua --获取场景 local scene= CCDirector:sharedDirector():getRunningScene() --创建精灵 local tes ...

  6. 高级程序员与CTO技术总监首席架构师

    一.高级程序员 如果你是一个刚刚创业的公司,公司没有专职产品经理和项目经理,你就是公司的产品经理,你如果对你现在的开发员能力不满,那么你只需要的是一个高级程序员. 你定义功能.你做计划推进和管理,他可 ...

  7. P3809 【模版】后缀排序

    题目背景 这是一道模版题. 题目描述 读入一个长度为 nn 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编 ...

  8. Java获取环境变量和系统属性

    Java获取服务器环境变量和JVM系统变量    当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty() ...

  9. MyBatis数据持久化(五)数据源配置优化

    在前面的教程中,我们把数据库的驱动.用户名.密码等配置项全部写在 SqlMapConfig.xml中: <dataSource type="POOLED"> <p ...

  10. ASP.NET使用MergeInto做数据同步,同步SQLSERVER不同数据库的相同表结构的数据

    public string SynchronousData() { ReturnJson Rejson = new ReturnJson(); //将WebConfig中的数据库连接name中的值写进 ...