36.Android之多线程和handle更新UI学习

android经常用到多线程更新UI,今天学习下.

首先布局比较简单:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ScrollView
 8         android:id="@+id/scrollView1"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" >
11 
12         <LinearLayout
13             android:layout_width="match_parent"
14             android:layout_height="wrap_content"
15             android:orientation="vertical" >
16 
17             <TextView
18                 android:id="@+id/tv"
19                 android:layout_width="match_parent"
20                 android:layout_height="wrap_content"
21                 android:text="TextView" />
22 
23         </LinearLayout>
24     </ScrollView>
25     
26  </LinearLayout>

增加一个读取文件线程类:

 1 package com.example.mulitthreadactivitydemo;
 2 
 3 import java.io.RandomAccessFile;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import android.util.Log;
 8 
 9 public class FileRead {
10 
11     boolean readend = false;
12     List<String> al = null;
13     private static final String Tag = "addfile";
14 
15     public class ReadNodesThread extends Thread {// 读取线程
16 
17         public void run() {
18             al = new ArrayList<String>(100);
19             al.clear();
20             readend = false;
21             int i = 0;
22             try {
23                 String dirstr = "/storage/sdcard0/DCIM/test.txt";
24                 //RandomAccessFile raf = new RandomAccessFile("/sdcard/test.txt","r");
25                 RandomAccessFile raf = new RandomAccessFile(dirstr,"r");
26                 // try {
27                 while (raf.getFilePointer() < raf.length()) {
28                     al.add(raf.readLine());
29                     Log.i(Tag,"addfile = " + raf.readLine());
30                     // sleep(100);//如果测试文件太小,这里休眠是为了测试,
31                 }
32 
33             } catch (Exception e1) {
34                 // TODO Auto-generated catch block      
35                 e1.printStackTrace();
36             }
37             readend = true;
38         }
39     };
40 
41 }

最后修改下主类:

 1 package com.example.mulitthreadactivitydemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.widget.TextView;
 7   
 8 public class MainActivity extends Activity {
 9     
10     FileRead fr=null;
11     Handler mHandler=null;
12     int curi=0;
13     Runnable updateui=null;
14     String[] tmp=null;
15     String s="";
16     TextView tv=null;
17     
18     class ReadListener extends Thread{//监听线程,当数据更新数目大于10条时,更新UI
19 
20         public void run()
21         {
22             int i=0,newi=0;
23             while(!fr.readend)
24             {
25                 newi=fr.al.size();
26                 if((newi-i)>10)//新增数据大于10条,更新UI
27                 {
28                     i=newi;
29                     tmp=(String[])fr.al.toArray(new String[fr.al.size()]);
30                     mHandler.post(updateui);
31                     try {
32                         Thread.sleep(100);
33                     } catch (InterruptedException e) {
34                         // TODO Auto-generated catch block
35                         e.printStackTrace();
36                     }
37                 }
38             }
39             //数据读完了
40             tmp=(String[])fr.al.toArray(new String[fr.al.size()]);
41             mHandler.post(updateui);
42             try {
43                 Thread.sleep(100);
44             } catch (InterruptedException e) {
45                 // TODO Auto-generated catch block
46                 e.printStackTrace();
47             }
48         }
49     };
50     
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.activity_main);
55         tv = (TextView) findViewById(R.id.tv);    
56         fr=new FileRead();        
57         FileRead.ReadNodesThread readThread = fr.new ReadNodesThread();
58         updateui = new Runnable()//更新UI的线程
59         {
60             @Override
61             public void run() {
62                 // TODO Auto-generated method stub
63 
64                 int i=0;
65 
66                 for(i=curi;i<tmp.length;i++)
67                 {
68                     s+=tmp[i]+"\n";
69                 }
70                 tv.setText(s);
71                 curi=i;
72             }};
73         readThread.start();
74         ReadListener updateThread=new ReadListener();
75         mHandler=new Handler();
76         updateThread.start();
77     
78     }
79 
80 }

 

运行效果:可以看到滚动条慢慢变短,则说明程序加载成功.

 

 

posted @ 2016-02-17 18:16  chaoer  阅读(303)  评论(0编辑  收藏  举报