android两种方式获取AsyncTask返回值
获取AsyncTask返回值,在Activity中使用。
引用链接:https://www.oschina.net/code/snippet_725438_49858#72630
[1].[代码] [Java]代码 跳至 [1] [2] [3] [4]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
布局文件:<LinearLayout 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:orientation="vertical" > <ImageView android:id="@+id/im1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/im2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /></LinearLayout> |
[2].[代码] [Java]代码 跳至 [1] [2] [3] [4]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
2.第一种,回调方法方式:package com.androidwallpaper;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.net.Uri;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.app.WallpaperManager;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{ ImageView im1; ImageView im2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); im1 = (ImageView) findViewById(R.id.im1); im2 = (ImageView) findViewById(R.id.im2); try { final ImageViewAsyncTask task = new ImageViewAsyncTask("http://static.oschina.net/uploads/ad/new_banner_one_ronglianyun_WrqUs.png"); task.setOnDataFinishedListener(new OnDataFinishedListener() { @Override public void onDataSuccessfully(Object data) { try { im1.setImageBitmap((Bitmap) data); } catch (Exception e) { e.printStackTrace(); } } @Override public void onDataFailed() { Toast.makeText(MainActivity.this, "加载失败!", Toast.LENGTH_SHORT).show(); } }); task.execute(); } catch (Exception e) { e.printStackTrace(); } } class ImageViewAsyncTask extends AsyncTask<String, Integer, Bitmap> { String mUrl; OnDataFinishedListener onDataFinishedListener; public ImageViewAsyncTask(String url){ this.mUrl = url; } public void setOnDataFinishedListener( OnDataFinishedListener onDataFinishedListener) { this.onDataFinishedListener = onDataFinishedListener; } @Override protected Bitmap doInBackground(String... params) { InputStream ins = null; Bitmap bitmap = null; try { URL url = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){ ins = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(ins); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(ins!=null){ try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override protected void onPreExecute() { super.onPreExecute(); progressBar.setVisibility(View.VISIBLE); } @Override protected void onPostExecute(Bitmap result) { progressBar.setVisibility(View.GONE); if(result!=null){ onDataFinishedListener.onDataSuccessfully(result); }else{ onDataFinishedListener.onDataFailed(); } } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } }} |
[3].[代码] [Java]代码 跳至 [1] [2] [3] [4]
|
1
2
3
4
5
6
7
8
|
回调接口:public interface OnDataFinishedListener { public void onDataSuccessfully(Object data); public void onDataFailed(); } |
[4].[代码] [Java]代码 跳至 [1] [2] [3] [4]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
第二种:使用handler:1.修改ImageViewAsyncTask:public class ImageViewAsyncTask extends AsyncTask<String, Integer, Bitmap> { String mUrl; Handler mHandler; public ImageViewAsyncTask(String url,Handler handler){ this.mUrl = url; this.mHandler = handler; } @Override protected Bitmap doInBackground(String... params) { InputStream ins = null; Bitmap bitmap = null; try { URL url = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){ ins = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(ins); return bitmap; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(ins!=null){ try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); Message msg = mHandler.obtainMessage(); if(result!=null){ msg.what = 1; msg.obj = result; }else{ msg.what = 2; } mHandler.sendMessage(msg); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } }2.调用方式:ImageViewAsyncTask task2 = new ImageViewAsyncTask("http://static.oschina.net/uploads/ad/new_banner_one_ronglianyun_WrqUs.png", handler); task2.execute();Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: Bitmap bitmap = (Bitmap) msg.obj; im2.setImageBitmap(bitmap); break; default: break; } } }; |
android两种方式获取AsyncTask返回值的更多相关文章
- Android 两种方式实现类似水波扩散效果
原文链接 https://mp.weixin.qq.com/s/M19tp_ShOO6esKdozi7Nlg 两种方式实现类似水波扩散效果,先上图为敬 自定义view实现 动画实现 自定义view实现 ...
- C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换
使用gmtime函数或localtime函数将time_t类型的时间日期转换为structtm类型: 使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年.月.日 ...
- .Net反射-两种方式获取Enum中的值
public enum EJobType { 客服 = , 业务员 = , 财务 = , 经理 = } Type jobType = typeof(EJobType); 方式1: Array enum ...
- android手机两种方式获取IP地址
1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...
- **CI两种方式查询所返回的结果数量
区别:第一个是有条件的:第二个没有条件 $this->db->count_all_results(); 允许你获得某个特定的Active Record查询所返回的结果数量.可以使用Acti ...
- Android Activity返回键控制的两种方式
Android Activity返回键监听的两种方式 1.覆写Activity的OnBackPressed方法 官方解释: Called when the activity has detected ...
- 获取Executor提交的并发执行的任务返回结果的两种方式/ExecutorCompletionService使用
当我们通过Executor提交一组并发执行的任务,并且希望在每一个任务完成后能立即得到结果,有两种方式可以采取: 方式一: 通过一个list来保存一组future,然后在循环中轮训这组future,直 ...
- drupal7 覆写node-type.tpl.php获取字段值的两种方式
字段的机读名称为:field_publication_date <!-- 下面两种方式都可以获取node字段的值--> 出版时间: <?php print date('Y-m-d', ...
- JavaWeb后台从input表单获取文本值的两种方式
JavaWeb后台从input表单获取文本值的两种方式 #### index.html <!DOCTYPE html> <html lang="en"> & ...
随机推荐
- 调式WP程序报0x80131500错误的解决办法
在虚拟机上安装了win8系统和VS2013,但是在允许第一个WP程序时,居然报0x80131500错误信息,经查询原来是VS2013需更新的问题,如果你用的是VS2012,但是又系统升级到了win8. ...
- WisDom .net开发框架设计 2
随笔- 10 文章- 0 评论- 57 WisDom .net开发框架设计 (二) WisDom .net 权限设计 1.前言 几乎在所有的管理的系统,都离不开用户,角色,权 ...
- 《剑指Offer》面试题-二维数组中的查找
题目1384:二维数组中的查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7318 解决:1418 题目描述: 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到 ...
- 活动图activity diagram
活动图activity diagram 系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend) [UML]UML系列——类 ...
- C# 利用反射动态创建对象——带参数的构造函数和String类型
C# 利用反射动态创建对象——带参数的构造函数和String类型 最近笔者有一个想法需要利用反射动态创建对象(如string,int,float,bool,以及自定义类等)来实现,一直感觉反射用不好, ...
- C#socket通信1
.net平台下C#socket通信(上) 完全是基础,新手可以随意看看,大牛可以关闭浏览页了,哈哈. 在开始介绍socket前先补充补充基础知识,在此基础上理解网络通信才会顺理成章,当然有基础的可以跳 ...
- iOS LBS相关: 定位和中国特色的位置偏移纠正
LBS模块,首先当然是定位,获取自己所在的位置.主要用到的就是CLLocationManager,实例一个,然后调用startUpdatingLocation即可.其中可以指定精度CLLocation ...
- ios学习之category设计模式
之前看书的时候,没怎么注意,但在项目中,才发现它的特别之处. 先来看看他用途:官网大意是这样写的:当你想简单的向一个已知类添加一个方法的时候,你就可以使用它.使用它的时候,命名是有要求的,如下: @i ...
- DSP TMS320C6000基础学习(4)—— cmd文件分析
DSP中的CMD文件是链接命令文件(Linker Command File),以.cmd为后缀. 在分析cmd文件之前,必需先了解 (1)DSP具体芯片的内存映射(Memory Map) (2)知道点 ...
- WPF/Silverlight中的RichTextBox总结
WPF/Silverlight中的RichTextBox总结 在WPF或者是在Silverlight中有个非常强大的可以编辑的容器控件RichTextBox,有的时间会采取该控件来作为编辑控件.鉴 ...