當前位置:首頁 » 安卓系統 » android非同步載入

android非同步載入

發布時間: 2022-05-04 06:13:28

① Android 非同步載入數據 創建子進程下載數據,ListView第一次載入無數據,第二次載入載才有數據

因為是非同步的,你下載完數據;需要再 進行 adapter.notifyDataSetChanged();

② android 二級緩存和非同步載入的區別

目前沒有同步載入數據這種做法,如果網路延遲主界面UI就卡死了,
之後用戶不耐煩就只能強行關閉了,卡死的時候按鍵都沒反應的。
一個簡單的的多線程
class
updatelocationTask
extends
AsyncTask<String,
Integer,
Response>
{
protected
void
onPreExecute()
{
//這里寫執行doInBackground方法之前要做的什麼,比如說彈出ProgressDialog
}
}
@Override
protected
Response
doInBackground(String...
params)
{
//這里就是線程裡面的方法了,比如說建立連接,請求數據
}
}
protected
void
onPostExecute(Response
result)
{
//這里可以根據返回值來確定怎麼操作,比如說刷新列表或者提示用戶網路不暢,是否再次刷新
}
}
}
}

③ android adapter是非同步載入的嗎

1、Adapter不屬於非同步載入,獲取Adapter需要的數據後,在UI線程中調用setAdapter()設置數據,網路一下TeachCourse,就知道了!

④ 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)從 線程隊列中移除線程對象,使線程停止執行。

⑤ 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);

⑥ android 什麼是非同步載入

因為我們都知道在Android中的是單線程模型,不允許其他的子線程來更新UI,只允許UI線程(主線程更新UI),否則會多個線程都去更新UI會造成UI的一個混亂有些耗時的操縱(例如網路請求等),如果直接放到主線程中去請求的話則會造成主線程阻塞,而我們系統有規定的響應時間,當響應的時間超過了了阻塞的時間就會造成"Application No Response",也就是我們熟知的ANR錯誤解決上述問題的時候:我們一般使用的是線程或者線程池+Handler機制如果線程拿到一個數據需要去更新UI,那麼就需要Handler把子線程的更新UI的數據發消息給主線程,從而讓主線程去更新UI那麼還在使用Thread或ThreadPool+Handler的你是否已經厭倦這些繁瑣的操縱而且你會發現這些操作的代碼都很類似。所以AsyncTask就應運而生了。

⑦ android中實現非同步操作的方式有哪些,請簡述各自的特點

  1. AsyncTask類。這是安卓自帶的非同步載入類,封裝較好,使用方便

  2. 直接開啟子線程通過handler發送消息更新,代碼繁瑣一點,AsyncTask源碼也是這個原理

⑧ 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) {
//這里可以根據返回值來確定怎麼操作,比如說刷新列表或者提示用戶網路不暢,是否再次刷新
}
}
}
}

⑨ android service非同步網路載入怎麼實現

**
* 封裝ProecssDialog對話框
*
*/
public class LoadDialog extends ProgressDialog {
private String title = "進度對話框";
private String message = "載入數據中....";
public LoadDialog(Context context, int theme) {
super(context, theme);
}

/**
* 用默認的標題和內容來創建對話框
* @param context
*/
public LoadDialog(Context context) {
super(context);
initDialog();
}
/**
* 用指定的標題和內容來創建對話框
* @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();
}
/**
* 初始化對話框參數,默認對話框不可以取消
*/
public void initDialog(){
setTitle(title);
setMessage(message);
setProgressStyle(ProgressDialog.STYLE_SPINNER);
setCancelable(false);
}
/**
* 打開對話框,設置回調方法,傳遞需要執行業務方法的類模板,方法名和參數列表
* @param callback 回調方法,該方法在對話框關閉後回調,並獲取返回的數據
* @param serviceClass 執行業務方法的類模板
* @param method 執行業務方法的方法名
* @param params 執行業務方法的參數列表
*/
public void execute(Callback callback,Class serviceClass,String method,Object... params){
super.show();
ServiceAysnTask task = new ServiceAysnTask(callback,serviceClass,method);
task.execute(params);
}

/**
* 回調方法的介面
*
*/
public interface Callback{
public void getResult(Map map);
}

/**
* 與遠程服務通信的線程類
* @author BDK
* AsyncTask 非同步任務
*/
private class ServiceAysnTask extends AsyncTask<object,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();//創建類模板對象
Class [] paramTypes = new Class[params.length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = params[i].getClass();
}
//根據類模板得到方法
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(), "網路通信異常", Toast.LENGTH_LONG).show();
return;
}
callback.getResult(result);
}
}
}
</object,object,map>

熱點內容
09壓縮餅干 發布:2025-05-15 05:05:58 瀏覽:279
迭代法編程c 發布:2025-05-15 04:58:01 瀏覽:815
用什麼dns伺服器地址快 發布:2025-05-15 04:52:59 瀏覽:27
手機端so反編譯 發布:2025-05-15 04:50:55 瀏覽:610
linuxlamp安裝 發布:2025-05-15 04:50:45 瀏覽:578
sqlplus緩存區怎麼設置 發布:2025-05-15 04:50:44 瀏覽:858
shell腳本環境變數 發布:2025-05-15 04:45:18 瀏覽:693
安卓nba2k18什麼時候出 發布:2025-05-15 04:38:42 瀏覽:393
王者安卓轉蘋果為什麼顯示失敗 發布:2025-05-15 04:35:49 瀏覽:18
手機優酷緩存視頻格式 發布:2025-05-15 04:13:45 瀏覽:210