android经常用到多线程更新UI,今天学习下.
首先布局比较简单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>
</ScrollView> </LinearLayout>
增加一个读取文件线程类:
package com.example.mulitthreadactivitydemo; import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List; import android.util.Log; public class FileRead { boolean readend = false;
List<String> al = null;
private static final String Tag = "addfile"; public class ReadNodesThread extends Thread {// 读取线程 public void run() {
al = new ArrayList<String>(100);
al.clear();
readend = false;
int i = 0;
try {
String dirstr = "/storage/sdcard0/DCIM/test.txt";
//RandomAccessFile raf = new RandomAccessFile("/sdcard/test.txt","r");
RandomAccessFile raf = new RandomAccessFile(dirstr,"r");
// try {
while (raf.getFilePointer() < raf.length()) {
al.add(raf.readLine());
Log.i(Tag,"addfile = " + raf.readLine());
// sleep(100);//如果测试文件太小,这里休眠是为了测试,
} } catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
readend = true;
}
}; }
最后修改下主类:
package com.example.mulitthreadactivitydemo; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView; public class MainActivity 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{//监听线程,当数据更新数目大于10条时,更新UI public void run()
{
int i=0,newi=0;
while(!fr.readend)
{
newi=fr.al.size();
if((newi-i)>10)//新增数据大于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();
}
}
}
//数据读完了
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
fr=new FileRead();
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<tmp.length;i++)
{
s+=tmp[i]+"\n";
}
tv.setText(s);
curi=i;
}};
readThread.start();
ReadListener updateThread=new ReadListener();
mHandler=new Handler();
updateThread.start(); } }
运行效果:可以看到滚动条慢慢变短,则说明程序加载成功.