当前位置:操作系统 > 安卓/Android >>

Android 异步加载解决方案

Android的Lazy Load主要体现在网络数据(图片)异步加载、数据库查询、复杂业务逻辑处理以及费时任务操作导致的异步处理等方面。在介绍Android开发过程中,异步处理这个常见的技术问题之前,我们简单回顾下Android开发过程中需要注意的几个地方。
Android应用开发过程中必须遵循单线程模型(Single Thread Model)的原则。因为Android的UI操作并不是线程安全的,所以涉及UI的操作必须在UI线程中完成。但是并非所有的操作都能在主线程中进行,Google工程师在设计上约定,Android应用在5s内无响应的话会导致ANR(Application Not Response),这就要求开发者必须遵循两条法则:1、不能阻塞UI线程,2、确保只在UI线程中访问Android UI工具包。于是,开启子线程进行异步处理的技术方案应运而生。

本文以自定义ListView,异步加载网络图片示例,总结了Android开发过程中,常用的三种异步加载的技术方案。

相关资源:

AndroidManifest.xml

01
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
02
    package="com.doodle.asycntasksample"
03
    android:versionCode="1"
04
    android:versionName="1.0" >
05
 
06
    <uses-sdk
07
        android:minSdkVersion="8"
08
        android:targetSdkVersion="15" />
09
 
10
    <uses-permission android:name="android.permission.INTERNET" />
11
 
12
    <application
13
        android:icon="@drawable/ic_launcher"
14
        android:label="@string/app_name"
15
        android:theme="@style/AppTheme" >
16
        <activity
17
            android:name="com.doodle.asynctasksample.ThreadHandlerPostActivity" >
18
        </activity>
19
        <activity android:name="com.doodle.asynctasksample.AsyncTastActivity" >
20
        </activity>
21
        <activity android:name="com.doodle.asynctasksample.ThreadHandlerActivity" >
22
        </activity>
23
        <activity
24
            android:name="com.doodle.asynctasksample.BootActivity"
25
            android:label="@string/title_activity_boot" >
26
            <intent-filter>
27
                <action android:name="android.intent.action.MAIN" />
28
                <category android:name="android.intent.category.LAUNCHER" />
29
            </intent-filter>
30
        </activity>
31
    </application>
32
 
33
</manifest>
list_item.xml
01
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02
    xmlns:tools="http://schemas.android.com/tools"
03
    android:layout_width="match_parent"
04
    android:layout_height="match_parent" >
05
 
06
    <LinearLayout
07
        android:layout_width="match_parent"
08
        android:layout_height="150dp"
09
        android:layout_alignParentLeft="true"
10
        android:layout_alignParentRight="true"
11
        android:layout_alignParentTop="true" >
12
 
13
        <ImageView
14
            android:id="@+id/imageView"
15
            android:layout_width="match_parent"
16
            android:layout_height="match_parent"
17
            android:src="<a href="http://my.oschina.net/asia" target="_blank" rel="nofollow">@android</a> :drawable/alert_dark_frame" />
18
 
19
    </LinearLayout>
20
 
21
</RelativeLayout>
ImageAdapter.java
01
/**
02
 * Create a customized data structure for each item of ListView.
03
 * An ImageAdapter inherited from BaseAdapter must overwrites
04
 * getView method to show every image in specified style.In this
05
 * instance only a ImageView will put and fill in each item of
06
 * ListView.
07
 *
08
 * @author Jie.Geng Aug 01, 2012.
09
 *
10
 */
11
public class ImageAdapter extends BaseAdapter {
12
    private Context context;
13
    private List<HashMap<String, Object>> listItems;
14
    private LayoutInflater listContainer;
15
    
16
    public ImageView imageView;
17
    
18
    public ImageAdapter(Context context, List<HashMap<String, Object>> listItems) {
19
        super();
20
        this.context = context;
21
        this.listContainer = LayoutInflater.from(context);
22
        this.listItems = listItems;
23
    }
24
 
25
    @Override
26
    public int getCount() {
27
        return listItems.size();
28
    }
29
 
30
    @Override
31
    public Object getItem(int position) {
32
        return null;
33
    }
34
 
35
    @Override
36
    public long getItemId(int position) {
37
        return 0;
38
    }
39
 
40
    @Override
41
    public View getView(int position, View convertView, ViewGroup pare

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,