目前沒有同步加載數(shù)據(jù)這種做法,如果網(wǎng)絡(luò)延遲主界面UI就卡死了,
墾利網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),墾利網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為墾利1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的墾利做網(wǎng)站的公司定做!
之后用戶不耐煩就只能強(qiáng)行關(guān)閉了,卡死的時候按鍵都沒反應(yīng)的。
一個簡單的的多線程
class updatelocationTask extends AsyncTaskString, Integer, Response {
protected void onPreExecute() {
//這里寫執(zhí)行doInBackground方法之前要做的什么,比如說彈出ProgressDialog
}
}
@Override
protected Response doInBackground(String... params) {
//這里就是線程里面的方法了,比如說建立連接,請求數(shù)據(jù)
}
}
protected void onPostExecute(Response result) {
//這里可以根據(jù)返回值來確定怎么操作,比如說刷新列表或者提示用戶網(wǎng)絡(luò)不暢,是否再次刷新
}
}
}
}
子線程沒有控制并發(fā)數(shù)量,當(dāng)并發(fā)過多的時候異步方法的作用就體現(xiàn)出來了。
異步是相對于同步而言的,顧名思義,同步就是各個通訊節(jié)點(diǎn)之間有統(tǒng)一的時鐘,按照相同的時鐘工作,異步相反,各節(jié)點(diǎn)之間沒有統(tǒng)一的時鐘,每個節(jié)點(diǎn)按照自己內(nèi)部的時鐘工作。
android在所有Thread當(dāng)中,有一個Thread,我們稱之為UI Thread。UI
Thread在Android程序運(yùn)行的時候就被創(chuàng)建,是一個Process當(dāng)中的主線程Main
Thread,主要是負(fù)責(zé)控制UI界面的顯示、更新和控件交互。在Android程序創(chuàng)建之初,一個Process呈現(xiàn)的是單線程模型,所有的任務(wù)都在一個線程中運(yùn)行。因此,我們認(rèn)為,UI
Thread所執(zhí)行的每一個函數(shù),所花費(fèi)的時間都應(yīng)該是越短越好。而其他比較費(fèi)時的工作(訪問網(wǎng)絡(luò),下載數(shù)據(jù),查詢數(shù)據(jù)庫等),都應(yīng)該交由子線程去執(zhí)行,以免阻塞主線程。
1、Adapter不屬于異步加載,獲取Adapter需要的數(shù)據(jù)后,在UI線程中調(diào)用setAdapter()設(shè)置數(shù)據(jù),百度一下TeachCourse,就知道了!
因?yàn)槭钱惒降?,你下載完數(shù)據(jù);需要再 進(jìn)行 adapter.notifyDataSetChanged();
以自定義ListView,異步加載網(wǎng)絡(luò)圖片示例,總結(jié)了Android開發(fā)過程中,常用的三種異步加載的技術(shù)方案。
相關(guān)資源:
manifest?xmlns: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=""????
02????????xmlns: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=""?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?ListHashMapString,?Object?listItems;????
14????????private?LayoutInflater?listContainer;????
15?????????????
16????????public?ImageView?imageView;????
17?????????????
18????????public?ImageAdapter(Context?context,?ListHashMapString,?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?parent)?{????
42????????????if(convertView?==?null)?{????
43????????????????convertView?=?listContainer.inflate(R.layout.list_item,?null);????
44????????????????imageView?=?(ImageView)?convertView.findViewById(R.id.imageView);????
45????????????????convertView.setTag(imageView);????
46????????????}?else?{????
47????????????????imageView?=?(ImageView)?convertView.getTag();????
48????????????}????
49????????????imageView.setImageDrawable((Drawable)?listItems.get(position).get("ItemImage"));????
50????????????return?convertView;????
51????????}
Handler簡介 Handler為Android提供了一種異步消息處理機(jī)制,它包含兩個隊(duì)列,一個是線程列隊(duì),另一個是消息列隊(duì)。使用post方法將線 程對象添加到線程隊(duì)列中,使用sendMessage(Message message)將消息放入消息隊(duì)列中。當(dāng)向消息隊(duì)列中發(fā)送消息后就立 即返回,而從消息隊(duì)列中讀取消息對象時會阻塞,繼而回調(diào)Handler中public void handleMessage(Message msg)方法。因此 在創(chuàng)建Handler時應(yīng)該使用匿名內(nèi)部類重寫該方法。如果想要這個流程一直執(zhí)行的話,可以再run方法內(nèi)部執(zhí)行postDelay或者 post方法,再將該線程對象添加到消息隊(duì)列中重復(fù)執(zhí)行。想要停止線程,調(diào)用Handler對象的removeCallbacks(Runnable r)從 線程隊(duì)列中移除線程對象,使線程停止執(zhí)行。
**
* 封裝ProecssDialog對話框
*
*/
public class LoadDialog extends ProgressDialog {
private String title = "進(jìn)度對話框";
private String message = "加載數(shù)據(jù)中....";
public LoadDialog(Context context, int theme) {
super(context, theme);
}
/**
* 用默認(rèn)的標(biāo)題和內(nèi)容來創(chuàng)建對話框
* @param context
*/
public LoadDialog(Context context) {
super(context);
initDialog();
}
/**
* 用指定的標(biāo)題和內(nèi)容來創(chuàng)建對話框
* @param context
* @param title
* @param message
*/
public LoadDialog(Context context,String title,String message){
super(context);
if(title != null){
this.title = title;
}
if(message != null){
this.message = message;
}
initDialog();
}
/**
* 初始化對話框參數(shù),默認(rèn)對話框不可以取消
*/
public void initDialog(){
setTitle(title);
setMessage(message);
setProgressStyle(ProgressDialog.STYLE_SPINNER);
setCancelable(false);
}
/**
* 打開對話框,設(shè)置回調(diào)方法,傳遞需要執(zhí)行業(yè)務(wù)方法的類模板,方法名和參數(shù)列表
* @param callback 回調(diào)方法,該方法在對話框關(guān)閉后回調(diào),并獲取返回的數(shù)據(jù)
* @param serviceClass 執(zhí)行業(yè)務(wù)方法的類模板
* @param method 執(zhí)行業(yè)務(wù)方法的方法名
* @param params 執(zhí)行業(yè)務(wù)方法的參數(shù)列表
*/
public void execute(Callback callback,Class serviceClass,String method,Object... params){
super.show();
ServiceAysnTask task = new ServiceAysnTask(callback,serviceClass,method);
task.execute(params);
}
/**
* 回調(diào)方法的接口
*
*/
public interface Callback{
public void getResult(Map map);
}
/**
* 與遠(yuǎn)程服務(wù)通信的線程類
* @author BDK
* AsyncTask 異步任務(wù)
*/
private class ServiceAysnTask extends AsyncTaskobject,object,map{
private Class serviceClass;
private String method;
private Callback callback;
public ServiceAysnTask(Callback callback,Class serviceClass,String method){
this.callback = callback;
this.serviceClass = serviceClass;
this.method = method;
}
@Override
protected Map doInBackground(Object... params) {
Map resultMap = null;
try {
Object obj = serviceClass.newInstance();//創(chuàng)建類模板對象
Class [] paramTypes = new Class[params.length];
for (int i = 0; i paramTypes.length; i++) {
paramTypes[i] = params[i].getClass();
}
//根據(jù)類模板得到方法
Method m = serviceClass.getMethod(method, paramTypes);
resultMap = (Map) m.invoke(obj, params);
} catch (Exception e) {
e.printStackTrace();
}
LoadDialog.this.cancel();
return resultMap;
}
@Override
protected void onPostExecute(Map result) {
super.onPostExecute(result);
if(result == null){
Toast.makeText(LoadDialog.this.getContext(), "網(wǎng)絡(luò)通信異常", Toast.LENGTH_LONG).show();
return;
}
callback.getResult(result);
}
}
}
/object,object,map