36.Android之多线程和handle更新UI学习
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(); } }
运行效果:可以看到滚动条慢慢变短,则说明程序加载成功.


36.Android之多线程和handle更新UI学习的更多相关文章
- Android中使用异步线程更新UI视图的几种方法
在Android中子线程是不能更新ui的. 所以我们要通过其他方式来动态改变ui视图, 1.runOnUiThreadactivity提供的一个轻量级更新ui的方法,在Fragment需要使用的时候要 ...
- Android中子线程真的不能更新UI吗?
Android的UI访问是没有加锁的,这样在多个线程访问UI是不安全的.所以Android中规定只能在UI线程中访问UI. 但是有没有极端的情况?使得我们在子线程中访问UI也可以使程序跑起来呢?接下来 ...
- 我对android handle更新UI 的一些理解
1.handle可以方便快捷地管理子线程对主线程UI 的更新, 2.如果不用handle,当多个子线程同时请求更新UI 时,UI更新操作就无法进行
- Android子线程真的不能更新UI么
Android单线程模型是这样描述的: Android UI操作并不是线程安全的,并且这些操作必须在UI线程执行 如果在其它线程访问UI线程,Android提供了以下的方式: Activity.run ...
- Android 通过广播来异步更新UI
之前的项目里要做一个异步更新UI的功能,可是结果出现了ANR,所以想写个demo来測试究竟是哪个地方出现了问题,结果发现原来的思路是没有问题,郁闷~~ 如今这个demo 就是模拟项目里面 的步骤 1. ...
- 在Android中实现service动态更新UI界面
之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...
- Android Handler传递参数动态更新UI界面demo
package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...
- Android在子线程中更新UI(二)
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
- Android在子线程中更新UI(一)
MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...
随机推荐
- unix文件操作函数
1. fopen函数 #include <stdio.h> FILE *fopen(const char *path, const char *mode) 返回:文件顺利打开后,指向该流的 ...
- javascript中的后退和刷新
<input type=button value=刷新 onclick="window.location.reload()"><input type=button ...
- Thread锁 Monitor类、Lock关键字和Mutex类
Monitor 类锁定一个对象 当多线程公用一个对象时,也会出现和公用代码类似的问题,这种问题就不应该使用lock关键字了,这里需要用到System.Threading中的一个类Monitor,我们可 ...
- 动态调用webservice,不需要添加Web References
using System; using System.Collections.Generic; using System.Web; using System.Net; using System.IO; ...
- iOS -数据库网络之xml解析之第三方解析XML
1.导入第三方插件(GDalaXMLNode) 2.第三方插件配置 libxml/tree.h 路径 在项目属性中--Bulid Settings中搜索 Search --Search ...
- 加密方式&数字签名
1,对称加密 2,混合加密 3.数字签名 4,带加密的数字签名
- c语言 &取地址运算符的理解
对于c语言中的&运算符,百度百科是这样定义的:(&p)则是这样一种运算,返回当时声明p 时开辟的地址:但是根据我对代码的观察,我觉得&运算符不只是返回地址的功能: 例如: in ...
- REST风格的原则
一个好的RESTful API,应该具备以下特征: 这个API应该是对浏览器友好的,能够很好地融入Web,而不是与Web格格不入. 浏览器是最常见和最通用的REST客户端.好的RESTful API应 ...
- Metasploit_01_信息搜集技术
信息搜集技术 姓名: 谈愈敏 学号: 20135220 日期: 2016.9.7 攻击机:135220-V.BT5, msf 靶 机:135220-V.W2k3_Sploitable 一.实验过程概述 ...
- poj2154-color-polyan次二面体+欧拉函数优化
N<=1e9,O(nlogn)的做法会超时.从枚举置换转变为枚举轮换长度,然后可以利用欧拉函数,把复杂度变为O(√n * logn) /*---------------------------- ...