博客
关于我
android解决:使用多线程和Handler同步更新UI
阅读量:366 次
发布时间:2019-03-05

本文共 2397 字,大约阅读时间需要 7 分钟。

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

   

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();
}
}

转载地址:http://jupg.baihongyu.com/

你可能感兴趣的文章
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>