日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android handler同步,android解决:使用多线程和Handler同步更新UI

發(fā)布時(shí)間:2025/3/12 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android handler同步,android解决:使用多线程和Handler同步更新UI 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如果運(yùn)行時(shí),可以看到滾動(dòng)條由條慢慢變短,則說(shuō)明程序成功了。截圖如下,建議選擇大點(diǎn)的文件做測(cè)試。

? ?

main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_height="wrap_content" android:id="@+id/scrollView1" android:layout_width="fill_parent">

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/tv"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

FileRead.java

public class FileRead {

boolean readend=false;

List al=null;

public ?class ReadNodesThread extends Thread{//讀取線程

public void run()

{

al=new ArrayList(100);

al.clear();

readend=false;

int i=0;

try {

RandomAccessFile raf=new RandomAccessFile("/sdcard/test.txt","r");

//try {

while(raf.getFilePointer()

{

al.add(raf.readLine());

//sleep(100);//如果測(cè)試文件太小,這里休眠是為了測(cè)試,

}

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

readend=true;

}

};

}

MultiThreadActivity.java

public class MultiThreadActivity extends Activity { FileRead fr=null; Handler mHandler=null; int curi=0; Runnable updateui=null; String[] tmp=null; String s=""; TextView tv=null; class ReadListener extends Thread{//監(jiān)聽(tīng)線程,當(dāng)數(shù)據(jù)更新數(shù)目大于10條時(shí),更新UI public void run() { int i=0,newi=0; while(!fr.readend) { newi=fr.al.size(); if((newi-i)>10)//新增數(shù)據(jù)大于10條,更新UI { i=newi; tmp=(String[])fr.al.toArray(new String[fr.al.size()]); mHandler.post(updateui); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //數(shù)據(jù)讀完了 tmp=(String[])fr.al.toArray(new String[fr.al.size()]); mHandler.post(updateui); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }? }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv=(TextView)findViewById(R.id.tv); fr=new FileRead(); ReadNodesThread readThread=fr.new ReadNodesThread(); updateui=new Runnable()//更新UI的線程 { @Override public void run() { // TODO Auto-generated method stub int i=0; for(i=curi;i

總結(jié)

以上是生活随笔為你收集整理的android handler同步,android解决:使用多线程和Handler同步更新UI的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。