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

android非同步調用

發布時間: 2024-07-19 05:18:37

Ⅰ 如何在Android開發中用AsyncTask非同步更新UI界面

在Android中實現非同步任務機制有兩種方式,Handler和AsyncTask。
Handler模式需要為每一個任務創建一個新的線程,任務完成後通過Handler實例向UI線程發送消息,完成界面的更新,這種方式對於整個過程的控制比較精細,但也是有缺點的,例如代碼相對臃腫,在多個任務同時執行時,不易對線程進行精確的控制。

為了簡化操作,Android1.5提供了工具類android.os.AsyncTask,它使創建非同步任務變得更加簡單,不再需要編寫任務線程和Handler實例即可完成相同的任務。
先來看看AsyncTask的定義:
public abstract class AsyncTask<Params, Progress, Result> {}

三種泛型類型分別代表「啟動任務執行的輸入參數」、「後台任務執行的進度」、「後台計算結果的類型」。在特定場合下,並不是所有類型都被使用,如果沒有被使用,可以用java.lang.Void類型代替。
一個非同步任務的執行一般包括以下幾個步驟:
1.execute(Params... params),執行一個非同步任務,需要我們在代碼中調用此方法,觸發非同步任務的執行。
2.onPreExecute(),在execute(Params... params)被調用後立即執行,一般用來在執行後台任務前對UI做一些標記。
3.doInBackground(Params... params),在onPreExecute()完成後立即執行,用於執行較為費時的操作,此方法將接收輸入參數和返回計算結果。在執行過程中可以調用publishProgress(Progress... values)來更新進度信息。
4.onProgressUpdate(Progress... values),在調用publishProgress(Progress... values)時,此方法被執行,直接將進度信息更新到UI組件上。
5.onPostExecute(Result result),當後台操作結束時,此方法將會被調用,計算結果將做為參數傳遞到此方法中,直接將結果顯示到UI組件上。

Ⅱ 在Android中什麼是非同步執行

我來給你講解一下非同步的使用吧,
如果你不是開發人員,直接跳到第三,非同步的概念 和 同步的區別:
一、在你的Activity中寫一個內部類:
private class TestAsyncTask extends AsyncTask<String, Void, Boolean>
{
@Override
protected void onPreExecute()
{
//最先執行的就是這個。
}

@Override
protected Boolean doInBackground(String... params)
{
//這個是在後台執行的東西,就是說,它自動另外開了個線程運行,不影響你現在做的東西。
}

@Override
protected void onPostExecute(Boolean result)
{
if (result)
{
//後台執行的完畢後,它會用Result通知這里,就是執行這里了。
}
else
{
//所以最好判斷一下result,寫個else,判斷後台執行的東西是不是出問題了。
}
}
}

二,在你的onCreate的時候啟動這個非同步,啟動代碼如下:
new TestAsyncTask().execute("");

三,非同步 和 同步的區別
非同步的好處,就是把一些東西,特別是耗時間的東西扔到後台去運行了,doInBackground,程序可以繼續做自己的事情,防止程序卡在那裡失去響應。
同步執行的話,就是程序會呆板地從頭執行到尾,耗時間的東西不執行完,程序不會繼續往下走,等待時間長的話,有時候就會造成失去響應了。

我就是搞開發的,呵呵。我的代碼你直接貼進去就能用的。打字貼代碼辛苦啊~~望採納。也歡迎追問

Ⅲ android activity 中的 service 方法是否非同步執行拜託各位大神

非同步執行
android中,activity、service都是在主線程,service與activity的主要區別就是service沒有前台界面,不能直接與用戶交互,另外可以相對保證不會被系統隨便的kill掉。所以service適用於一些無需交互的後台操作,但如果你直接在service中進行耗時操作的話,因為在主線程所以依然會出現和activity主線程一樣的超時的問題,所以好的方式是在service中啟動其他的線程去執行耗時操作。

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

以自定義ListView,非同步載入網路圖片示例,總結了Android開發過程中,常用的三種非同步載入的技術方案。

相關資源:

<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網路請求數據是同步還是非同步

非同步請求,因為UI線程(主線程)不允許有5秒以上的耗時操作.在主線程網路請求會導致阻塞,看起來程序就像假死了一樣.所以都是非同步請求.

熱點內容
ios應用上傳 發布:2024-09-08 09:39:41 瀏覽:439
ios儲存密碼哪裡看 發布:2024-09-08 09:30:02 瀏覽:873
opensslcmake編譯 發布:2024-09-08 09:08:48 瀏覽:653
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:744
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:173
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:780
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995