其实我们要牢记的是,对数据流的操作都是阻塞的,在一般情况下,我们是不需要考虑这个问题的,但是在Android 实现联网的时候,我们必须考虑到这个问题。比如:从网络上下载一张图片:

Java代码:

  1. public Bitmap returnBitmap(String url)
  2. {
  3. URL myFileUrl = null;
  4. Bitmap bitmap = null;
  5. try{
  6. myFileUrl = new URL(url);
  7. }catch(MalformedURLException e){
  8. e.printStackTrace();
  9. return null;
  10. };
  11. try{
  12. HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();
  13. conn.setDoInput(true);
  14. conn.connect();
  15. InputStream is = conn.getInputStream();
  16. bitmap = BitmapFactroy.decodeStream(is);
  17. is.close();
  18. }catch(IOException e){
  19. e.printStackTrace();
  20. }
  21. return bitmap;
  22. }

复制代码

由于网络连接需要很长的时间,需要3-5秒,甚至更长的时间才能返回页面的内容。如果此连接动作直接在主线程,也就是UI线程中处理,会发生什么情
况呢? 整个程序处于等待状态,界面似乎是“死”掉了。为了解决这个问题,必须把这个任务放置到单独线程中运行,避免阻塞UI线程,这样就不会对主线程有任何影
响。举个例子如下:

Java代码:

  1. private void connect(String strURL){
  2. new Thread() {
  3. public void run() {
  4. try {
  5. HttpClient client = new DefaultHttpClient();
  6. // params[0]代表连接的url
  7. HttpGet get = new HttpGet(url.getText().toString());
  8. HttpResponse response = client.execute(get);
  9. HttpEntity entity = response.getEntity();
  10. long length = entity.getContentLength();
  11. InputStream is = entity.getContent();
  12. String s = null;
  13. if (is != null) {
  14. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  15. byte[] buf = new byte[128];
  16. int ch = -1;
  17. int count = 0;
  18. while ((ch = is.read(buf)) != -1) {
  19. baos.write(buf, 0, ch);
  20. count += ch;
  21. }
  22. s = new String(baos.toByteArray());
  23. Log.V(“moandroid sample”,s);
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }.start();
  30. }

复制代码

使用Handler更新界面

如何将下载的信息显示在界面上了,比如说下载的进度。Android
SDK平台只允许在主线程中调用相关View的方法来更新界面。如果返回结果在新线程中获得,那么必须借助Handler来更新界面。为此,在界面 Activity中创建一个Handler对象,并在handleMessage()中更新UI。

Java代码:

  1. //Task在另外的线程执行,不能直接在Task中更新UI,因此创建了Handler
  2. private Handler handler = new Handler() {
  3. @Override
  4. public void handleMessage(Message msg) {
  5. String m = (String) msg.obj;
  6. message.setText(m);
  7. }
  8. };
  9. //只需要将上面的
  10. Log.V(“moandroid sample”,s);
  11. //替换为:
  12. s = new String(baos.toByteArray());
  13. Message mg = Message.obtain();
  14. mg.obj = s;
  15. handler.sendMessage(mg);

最新文章

  1. 初始化Git的配置
  2. SQL是关于集合的
  3. Spring源码学习之:模拟实现BeanFactory,从而说明IOC容器的大致原理
  4. VC++2010配置使用MySQL5.6
  5. el表达式获取cookie
  6. Qt: qobject_cast<QPushButton*>(sender()) 简化信号与槽的编写
  7. JS 动态显示 获取下拉框的多个值
  8. selenium简单代码入门
  9. axure rp pro 6.5
  10. Python模拟键盘输入和鼠标操作
  11. SharePoint Attachement操作代码
  12. Web控件
  13. SQLServer 2008的组成
  14. cocos2D(八)---- CCMenu && CCMenuItem
  15. Sample Page
  16. oracle 常用select sql语句
  17. js获取file控件的完整路径(上传图片预览)
  18. 《Linux就该这么学》第七天课程
  19. ng-cordova和cordova区别
  20. centos7环境搭建

热门文章

  1. 分区 Service Bus 队列和主题
  2. BZOJ 1058 报表统计 (STL)
  3. software quality assurance 常见问题收录
  4. 80x86汇编小站站长简单介绍
  5. hadoop学习;安装jdk,workstation虚拟机v2v迁移;虚拟机之间和跨物理机之间ping网络通信;virtualbox的centos中关闭防火墙和检查服务启动
  6. xampp中mysql设置密码
  7. java volatile关键字的理解
  8. Codeforces 492B B. Vanya and Lanterns
  9. BZOJ 1677: [Usaco2005 Jan]Sumsets 求和( dp )
  10. MyEclipse下JDBC-MySQL配置总结