AsyncTask基础
来源:互联网 发布:张艺兴发布会知乎 编辑:程序博客网 时间:2024/05/17 05:18
AsyncTask基本用法:
AsyncTask是一个抽象类,我们不能直接使用它,需要新建一个子类来继承AsyncTask,继承的时候可以指定三个泛型参数。
1、Params
在执行AsyncTask时需要传入的参数,可用于在后台任务中使用。
2、Progress
在后台任务执行中,作为进度条在界面上显示。
3、Result
当任务执行完毕后,如果需要对结果进行返回,则使用这里指定的泛型作为返回值类型。
下面让我们来看一下一个小案例,代码注释的比较清楚。先上图:
MainActivity.class:
package com.example.tony.anynctasktest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void loadImage(View view) { startActivity(new Intent(this, ImageTest.class)); }}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.example.tony.anynctasktest.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="loadImage" android:text="@string/image_text" android:textAllCaps="false" /></RelativeLayout>
ImageTest.class;
package com.example.tony.anynctasktest;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.ProgressBar;import java.io.BufferedInputStream;import java.io.IOException;import java.net.URL;//导入URL包import java.io.InputStream;import java.net.URLConnection;/** * Created by Tony on 16/11/15. */public class ImageTest extends Activity{ private ImageView mImageView; private ProgressBar mProgressBar; private static String URL = "http://img.mukewang.com/57a322f00001e4ae02560256-200-200.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image); mProgressBar = (ProgressBar) findViewById(R.id.progress); mImageView = (ImageView) findViewById(R.id.image); //通过MyAsyncTask实例调用execute方法开启AsyncTask的异步线程操作,在方法中传进去一个或多个参数 new MyAsyncTask().execute(URL); } class MyAsyncTask extends AsyncTask<String, Void, Bitmap> { @Override protected void onPreExecute() { super.onPreExecute(); //显示之前隐藏的progressbar,告知用户程序正在加载请等待。 mProgressBar.setVisibility(View.VISIBLE); } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); //获取到图像,操作UI,设置图像 mProgressBar.setVisibility(View.GONE);//隐藏progressbar mImageView.setImageBitmap(bitmap);//设置图像 } @Override protected Bitmap doInBackground(String... strings) { /** * 1、获取传递进来的参数 * 2、一步线程中处理访问网络操作,通过decodeStream解析输入流 * 3、将bitmap作为返回值返回给后面调用的方法 */ //1 String url = strings[0];//取出对应url; Bitmap bitmap = null; URLConnection connection; InputStream in; //2 try { connection = new URL(url).openConnection();//获取网络连接对象 in = connection.getInputStream();//获取输入流 BufferedInputStream buffer = new BufferedInputStream(in); //设置线程睡眠时间,不然加载太快看不到progressbar Thread.sleep(3000); bitmap = BitmapFactory.decodeStream(buffer);//将URL解析成图像 //解析完毕记得关闭输入流 in.close(); buffer.close(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } //3 return bitmap;//返回bitmap } }}
image.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" /> <ProgressBar android:id="@+id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:visibility="gone" /></RelativeLayout>
0 0
- AsyncTask基础
- asyncTask基础
- AsyncTask基础
- 【Android基础】AsyncTask基础
- AsyncTask的基础
- AsyncTask的基础
- AsyncTask的基础
- AsyncTask的基础
- [Android基础]AsyncTask类
- android基础--AsyncTask
- AsyncTask同步加载基础
- Android AsyncTask基础
- AsyncTask基础(笔记)
- AsyncTask基础<笔记>
- Android基础之AsyncTask
- AsyncTask基础(2)
- android基础(AsyncTask)
- AsyncTask的基础使用
- android 读取本地文件的一种方式
- http get 请求下载图片至本地
- this monitor is disabled问题解决
- EasyAR涂涂乐代码分析
- SCNScene类
- AsyncTask基础
- python 在linux 下面读取sybase 安装及配置 (亲测可用)
- HTTP 协议的通用头域via 的意义以及作用
- 移除所有符号
- [NOIP2009]普及组T4 道路游戏
- linux 线程
- 安装python的一些工具包---备忘录
- 白话设计模式《模板模式》《策略模式》《代理模式》
- Git常用命令