android的非同步載入
A. android 非同步載入有幾種方法
子線程沒有控制並發數量,當並發過多的時候非同步方法的作用就體現出來了。
非同步是相對於同步而言的,顧名思義,同步就是各個通訊節點之間有統一的時鍾,按照相同的時鍾工作,非同步相反,各節點之間沒有統一的時鍾,每個節點按照自己內部的時鍾工作。
android在所有Thread當中,有一個Thread,我們稱之為UI Thread。UI
Thread在Android程序運行的時候就被創建,是一個Process當中的主線程Main
Thread,主要是負責控制UI界面的顯示、更新和控制項交互。在Android程序創建之初,一個Process呈現的是單線程模型,所有的任務都在一個線程中運行。因此,我們認為,UI
Thread所執行的每一個函數,所花費的時間都應該是越短越好。而其他比較費時的工作(訪問網路,下載數據,查詢資料庫等),都應該交由子線程去執行,以免阻塞主線程。
B. android中listview的數據的同步與非同步載入有什麼區別,效果有什麼不同
目前沒有同步載入數據這種做法,如果網路延遲主界面UI就卡死了,
之後用戶不耐煩就只能強行關閉了,卡死的時候按鍵都沒反應的。
一個簡單的的多線程
class updatelocationTask extends AsyncTask<String, Integer, Response> {
protected void onPreExecute() {
//這里寫執行doInBackground方法之前要做的什麼,比如說彈出ProgressDialog
}
}
@Override
protected Response doInBackground(String... params) {
//這里就是線程裡面的方法了,比如說建立連接,請求數據
}
}
protected void onPostExecute(Response result) {
//這里可以根據返回值來確定怎麼操作,比如說刷新列表或者提示用戶網路不暢,是否再次刷新
}
}
}
}
C. android adapter是非同步載入的嗎
1、Adapter不屬於非同步載入,獲取Adapter需要的數據後,在UI線程中調用setAdapter()設置數據,網路一下TeachCourse,就知道了!
D. android開發,非同步載入數據被突然彈出的電話界面打斷,怎麼辦
非同步查詢數據時,被來電打斷,線程會拋出異常或者會被掛起,建議查看logcat中的日誌。
Android開發中,所的有輸出都在logcat中 包含System.out輸出和printStackTrace()輸出都在Logcat中,Android開發,建議使用android提供的Log工具類來列印信息。
找到Logcat視圖的方式:
1. Eclipse 點擊 Window。
2. Show View會出來一個對話框。
3. 點擊Ok按鈕時,會在控制台窗口出現LogCat視圖。
E. android中非同步載入圖片怎麼結束
如果你要是使用
wrap_content的話,那麼圖片大小肯定是不一定的,如果你要是想設置圖片大小的話
,你可以通過
<imageview
android:layout_width="50dp"
android:layout_height="50dp"
android:scaletype="fitxy"
/>
來設置!
如果你要是想通過代碼設置的話:
imageview
iv
=
(imageview)
findviewbyid(r.id.iv);
iv.setlayoutparams(new
layoutparams(300,
300));
iv.setscaletype(scaletype.fit_xy);
F. android非同步網路載入怎麼實現
以自定義ListView,非同步載入網路圖片示例,總結了Android開發過程中,常用的三種非同步載入的技術方案。
相關資源:
java"><manifestxmlns:android="http://schemas.android.com/apk/res/android"
02package="com.doodle.asycntasksample"
03android:versionCode="1"
04android:versionName="1.0">
05
06<uses-sdk
07android:minSdkVersion="8"
08android:targetSdkVersion="15"/>
09
10<uses-permissionandroid:name="android.permission.INTERNET"/>
11
12<application
13android:icon="@drawable/ic_launcher"
14android:label="@string/app_name"
15android:theme="@style/AppTheme">
16<activity
17android:name="com.doodle.asynctasksample.ThreadHandlerPostActivity">
18</activity>
19<activityandroid:name="com.doodle.asynctasksample.AsyncTastActivity">
20</activity>
21<activityandroid:name="com.doodle.asynctasksample.ThreadHandlerActivity">
22</activity>
23<activity
24android:name="com.doodle.asynctasksample.BootActivity"
25android:label="@string/title_activity_boot">
26<intent-filter>
27<actionandroid:name="android.intent.action.MAIN"/>
28<categoryandroid:name="android.intent.category.LAUNCHER"/>
29</intent-filter>
30</activity>
31</application>
32
33</manifest>
list_item.xml
01<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
02xmlns:tools="http://schemas.android.com/tools"
03android:layout_width="match_parent"
04android:layout_height="match_parent">
05
06<LinearLayout
07android:layout_width="match_parent"
08android:layout_height="150dp"
09android:layout_alignParentLeft="true"
10android:layout_alignParentRight="true"
11android:layout_alignParentTop="true">
12
13<ImageView
14android:id="@+id/imageView"
15android:layout_width="match_parent"
16android:layout_height="match_parent"
17android:src="<ahref="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*.
03*
04*.Inthis
05*
06*ListView.
07*
08*@authorJie.GengAug01,2012.
09*
10*/
{
12privateContextcontext;
13privateList<HashMap<String,Object>>listItems;
;
15
16publicImageViewimageView;
17
18publicImageAdapter(Contextcontext,List<HashMap<String,Object>>listItems){
19super();
20this.context=context;
21this.listContainer=LayoutInflater.from(context);
22this.listItems=listItems;
23}
24
25@Override
26publicintgetCount(){
27returnlistItems.size();
28}
29
30@Override
31publicObjectgetItem(intposition){
32returnnull;
33}
34
35@Override
36publiclonggetItemId(intposition){
37return0;
38}
39
40@Override
41publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
42if(convertView==null){
43convertView=listContainer.inflate(R.layout.list_item,null);
44imageView=(ImageView)convertView.findViewById(R.id.imageView);
45convertView.setTag(imageView);
46}else{
47imageView=(ImageView)convertView.getTag();
48}
49imageView.setImageDrawable((Drawable)listItems.get(position).get("ItemImage"));
50returnconvertView;
51}
Handler簡介 Handler為Android提供了一種非同步消息處理機制,它包含兩個隊列,一個是線程列隊,另一個是消息列隊。使用post方法將線 程對象添加到線程隊列中,使用sendMessage(Message message)將消息放入消息隊列中。當向消息隊列中發送消息後就立 即返回,而從消息隊列中讀取消息對象時會阻塞,繼而回調Handler中public void handleMessage(Message msg)方法。因此 在創建Handler時應該使用匿名內部類重寫該方法。如果想要這個流程一直執行的話,可以再run方法內部執行postDelay或者 post方法,再將該線程對象添加到消息隊列中重復執行。想要停止線程,調用Handler對象的removeCallbacks(Runnable r)從 線程隊列中移除線程對象,使線程停止執行。