如果运行时,可以看到滚动条由条慢慢变短,则说明程序成功了。截图如下,建议选择大点的文件做测试。


main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:id="@+id/scrollView1" android:layout_width="fill_parent">
<LinearLayout android:id="@+id/linearLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>

FileRead.java

public class FileRead {
boolean readend=false;
List<String> al=null;
public class ReadNodesThread extends Thread{//读取线程

public void run()
{
al=new ArrayList<String>(100);
al.clear();
readend=false;
int i=0;
try {
RandomAccessFile raf=new RandomAccessFile("/sdcard/test.txt","r");
//try {
while(raf.getFilePointer()<raf.length())
{
al.add(raf.readLine());
//sleep(100);//如果测试文件太小,这里休眠是为了测试,
}

} 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{//监听线程,当数据更新数目大于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
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<tmp.length;i++)
{
s+=tmp[i]+"\n";
}
tv.setText(s);
curi=i;
}};
readThread.start();
ReadListener updateThread=new ReadListener();
mHandler=new Handler();
updateThread.start();
}
}


更多相关文章

  1. Android(安卓)的消息机制(2)
  2. Android异步更新UI的方式之使用AsyncTask异步任务
  3. java 课堂总笔记
  4. 今天更新SDK到11
  5. Android多线程下安全访问数据库
  6. EventBus for Android(安卓)源码分析
  7. 理解Android核心:Looper, Handler和HandlerThread
  8. Android(安卓)和 前端的类似点(持续更新)
  9. Android(安卓)Launcher 应用的安装、更新、移除介绍

随机推荐

  1. Android输入系统之InputChannel(上)
  2. Android程序报R cannot be resolved to a
  3. Android 平台实现按指定平率和时长发声(
  4. android源生Browser分析(四)---Webkit部分
  5. 如何在Nokia-x中获得广告ID ?
  6. 在活动中管理多个asynctask什么显示数据(S
  7. 如何在函数中将两个参数从1个类传递给另
  8. kotlin-stdlib-jre7已弃用。请改用kotlin
  9. android BluetoothAdapter蓝牙BLE扫描总
  10. AutoCompleteTextView 设置了点击事件需