今天在做http网络事件的响应网络接收处理一般不能放在主线程中使用,目前也只会使用AsyncTask进行处理!之前虽然写过handler处理的一些文章但是发现全不会了!无奈~

关于handler某位兄弟已经整理的很透彻了!现在引用下原话如下:

Handler监听者框架:子线程是事件源,主线程是监听者。
Handler作为子线程的监听器出现:主线程中生成Handler的子类,并重写handleMessage(Message msg) 方法,用来对子线程响应。子线程调用Hanlder的sendMessage(message)发送事件。

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView; public class MainActivity extends Activity { private final static String TAG = "ThreadTest";
TextView myTv = null;
int i = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTv = (TextView) this.findViewById(R.id.tv1);
// 执行该线程
new Thread(runnable).start();
}
/**
* 主线程监听者
*/
Handler myHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "begin--->handleMessage");
if (msg.what == 0x000001) {
myTv.setText(String.valueOf(i));
}
Log.i(TAG, "end--->handleMessage");
super.handleMessage(msg);
};
}; /**
* 子线程事件源
*/
Runnable runnable = new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
i++;
Message msg = new Message();
msg.what = 0x000001;
Log.i(TAG, "begin--->sendMessage");
myHandler.sendMessage(msg);
Log.i(TAG, "end--->sendMessage");
Log.i(TAG, "begin--->dosomething");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "end--->dosomething");
}
}
}; }

打印下输出:发现它并不是等dosomething执行完毕之后再调用handlemessage事件,而是一旦调用sendmessage后就会执行handlemessage。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyUAAACECAIAAAABXhMeAAAgAElEQVR4nO2dy5njKhCFHRcBkUanoI2i0J69k9DGIXQQugvE+xQPW9juO+f/ZuHBPAqqgFJBW7eDEEIIIYTM5PZpAQghhBBC/ufQ3yKEEEIImQv9LUII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Q2BnDMXh0alZdX+ctxZ2fUvvvCdSJTdVtQkVahNVdYRrqje12kqkhuyfvmZypj//ge/JNh4nggCxPOsSKt9MppTgRaGGih7VWp/kb3kZfD2T+hU1pJW7zlK2LtSZqC/ub6YU0kvHvAvPPIXiXsLaEgzqFw2hM3dsDAMLkTDruyY4WkmkoYu2To3+HOor4O+dEkKeBv6lJyGEfIgvPvimv0UIeRIf2eLpEiHkk4RA2vf+li/9LUIIIYSQudDfIoQQQgiZC/0tQgghhJC50N8ihBBCCJkL/S1CCCGEkLnQ3yKEEEIImct/6gt5p4e0iZIAAAAASUVORK5CYII=" alt="" />

Handler钩子方式:在主线程中生成一个Hanlder,用Handler的Post(Runnable)方法可以将Runnable钩到主线程中运行。

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.widget.TextView; public class MainActivity extends Activity { private final static String TAG = "ThreadTest";
TextView myTv = null;
//handler在主线程中创建,调用post的时候钩子会把它钩回主线程
Handler myHandler = new Handler();
int i = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTv = (TextView) this.findViewById(R.id.tv1);
new Thread(runnable1).start();
} /**
* 子线程
*/
Runnable runnable1 = new Runnable() {
@Override
public void run() {
while (true) {
i++;
myHandler.post(runnable2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}; /**
* 使用钩子将该子线程钩到主线程中
*/
Runnable runnable2 = new Runnable() {
@Override
public void run() {
myTv.setText(String.valueOf(i));
}
}; }

对于我们有了这样子的一个handler,如果需要进行一个非常耗时的网络请求,也不必担心迟迟无法显示activity。因为下载的任务交给了我们的子线程。当完成任务的时候我们可以调用sendMessage方法,通知主线程更新UI。bingo!真是太棒了!

Android 再谈handler的更多相关文章

  1. 【转】 Pro Android学习笔记(四三):Fragment(8):再谈Transaction和管理器

    目录(?)[-] Transaction的一些操作 再谈FragmentManager 调用其他fragment的方法 唤起activity 唤起fragment和相互通信 一些其它 Transact ...

  2. 深入探讨Android异步精髓Handler

    探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架 ...

  3. Android正在使用Handler实现消息分发机制(零)

    演讲前,AsyncTask文章.我们在最后谈到.AsyncTask它是利用Handler异步消息处理机制,操作结果.使用Message回到主线程,从而执行UI更新线程. 而在我们的日常开发工作,Han ...

  4. (转)Android消息处理机制(Handler、Looper、MessageQueue与Message)

    转自 http://www.cnblogs.com/angeldevil/p/3340644.html Android消息处理机制(Handler.Looper.MessageQueue与Messag ...

  5. Android中关于Handler的若干思考

    在之前的博文中,讲过一些和Handler有关的知识,例如: Android 多线程----AsyncTask异步任务详解 Android多线程----异步消息处理机制之Handler详解 今天再把Ha ...

  6. android中的Handler和Runnable

    最近在做一个项目,在网络请求时考虑用Handler进行处理,然后就研究了一下Handler和Runnable 首先在看一下java中的Runnable The Runnable interface s ...

  7. Android学习笔记--Handler用法总结

    不错的例子:http://www.cnblogs.com/menlsh/archive/2013/06/07/3125341.html 转自:一叶知秋的博客 http://blog.sina.com. ...

  8. 再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结

    这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理汇总.如果对文中的题目全部了然于胸,觉得对整个angular框架应该掌握的七七八八了.希望志同道合的通知补充 ...

  9. 【网络】再谈select, iocp, epoll,kqueue及各种I/O复用机制 && Reactor与Proactor的概念

    首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblocking I/O I/O multiplexing (select and poll) signal drive ...

随机推荐

  1. UT源码 065

    NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. 要求 ...

  2. 模仿w3c school的示例导航栏

    近日学习HTML,恰巧学习过程中看见w3cschool的示例导航栏看上去很不错,适合新手练习,于是模仿着做了一个. 示例导航栏如下图所示 导航栏自然使用li标签来做,a标签控制背景颜色和鼠标放入的特效 ...

  3. How to use the NFS Client c# Library

    类库下载 I add a wiki page that explains how to use the NFS Client c# .net library in your project. Neko ...

  4. Spring Boot - Spring Cache

    缓存 服务器自身(内存)的缓存 利用java程序中的变量 简单 集群环境中多个实例无法共享同步 缓存服务器(一般支持集群.分布式) Redis Memcached Spring中使用注解使用缓存 启动 ...

  5. 零散知识点总结(持续更新……)

        这篇博客用于记录平时学习中遇到的零散的知识点,它们不适于单独写一篇长博客,在这里记录下来一是为了增强记忆,二是为了方便复习总结.这篇博客会持续更新... 一.JS数据类型及类型判断 1. JS ...

  6. [Perl]通过GD模块获取字符/汉字的点阵

    原来Perl获取汉字的点阵如此简单 示例脚本应保存为utf8格式 Code: [全选] [展开/收缩] [Download] (Untitled.pl) use GD; use utf8; my $g ...

  7. SDOI2013 淘金

    题目链接:戳我 昨天做的题了,今天补一发题解. 是一个比较奇怪的数位DP.详细的我还是写代码注释里好了,感觉直接说不好描述. 代码如下: #include<iostream> #inclu ...

  8. “全栈2019”Java第六十章:如何定义接口

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  9. php中使用PHPExcel读写excel(xls)文件的方法

    首先从GitHub上下载 excel的相关类库 下载地址:https://github.com/PHPOffice/PHPExcel 以下是从excel中获取数据 <?php /** * * @ ...

  10. Django 允许其他可以访问的设置

    第一步:在run下修改edit 第二步:host改为0.0.0.0 第三步:setting文件中将    ALLOWED_HOSTS 改为 :ALLOWED_HOSTS = ['*',] 这样就可以通 ...