消息通道:Looper

首先编写main.xml文件

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动"/>
</LinearLayout>

  然后改写Activity.java类

代码如下:

package com.example.myfirstproject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*; public class MainActivity extends Activity {
private TextView info;
private Button but;
private static final int SET = 1; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.info = (TextView)super.findViewById(R.id.info);
this.but = (Button)super.findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerlmpl());
}
private class OnClickListenerlmpl implements OnClickListener{
public void onClick(View view){
switch(view.getId()){
case R.id.but:
Looper looper = Looper.myLooper();
MyHandler myHandler = new MyHandler(looper);
myHandler.removeMessages(0);
String data = "hahahaha";
Message msg = myHandler.obtainMessage(SET,1,1,data);
myHandler.sendMessage(msg);
break;
}
}
}
private class MyHandler extends Handler{
public MyHandler(Looper looper){
super(looper);
}
public void handleMessage(Message msg){
switch(msg.what){
case 1:
MainActivity.this.info.setText(msg.obj.toString());
}
}
}
}

  运行效果:

android之消息机制(二)的更多相关文章

  1. 【原创】源码角度分析Android的消息机制系列(二)——ThreadLocal的工作过程

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一篇文章中,我们已经提到了ThreadLocal,它并非线程,而是在线程中存储数据用的.数据存储以后,只能在指定的线程中获取到数据,对于其 ...

  2. Android的消息机制

    一.简介 ①.我们不能在子线程中去访问UI空控件,这是时候只能通过Handler将更新UI的操作放到主线程中去执行 ②.Handler的组成:messageQueue和Looper的支持 ③.Mess ...

  3. Android 基础 十一 Android的消息机制

    Handler是Android消息机制的上层接口,这使得在开发应用过程中我们只需要和Handler交互即可.Handler的使用过程很简单,通过它可以轻松地将一个任务切换到Handler所在的线程中去 ...

  4. 《Android开发艺术探索》读书笔记 (10) 第10章 Android的消息机制

    第10章 Android的消息机制 10.1 Android消息机制概述 (1)Android的消息机制主要是指Handler的运行机制,其底层需要MessageQueue和Looper的支撑.Mes ...

  5. 【原创】源码角度分析Android的消息机制系列(一)——Android消息机制概述

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.为什么需要Android的消息机制 因为Android系统不允许在子线程中去访问UI,即Android系统不允许在子线程中更新UI. 为什 ...

  6. 【原创】源码角度分析Android的消息机制系列(五)——Looper的工作原理

    ι 版权声明:本文为博主原创文章,未经博主允许不得转载. Looper在Android的消息机制中就是用来进行消息循环的.它会不停地循环,去MessageQueue中查看是否有新消息,如果有消息就立刻 ...

  7. Android的消息机制简单总结

    参考文章: http://gityuan.com/2015/12/26/handler-message-framework/#next 参考资料: Android Framework的源码: Mess ...

  8. 聊一聊Android的消息机制

    聊一聊Android的消息机制 侯 亮 1概述 在Android平台上,主要用到两种通信机制,即Binder机制和消息机制,前者用于跨进程通信,后者用于进程内部通信. 从技术实现上来说,消息机制还是比 ...

  9. Android开发——Android的消息机制详解

    )子线程默认是没有Looper的,Handler创建前,必须手动创建,否则会报错.通过Looper.prepare()即可为当前线程创建一个Looper,并通过Looper.loop()来开启消息循环 ...

  10. [转]Android进程间通信--消息机制及IPC机制实现

    Android为了屏蔽进程的概念,利用不同的组件[Activity.Service]来表示进程之间的通信! 组件间通信的核心机制是Intent,通过Intent可以开启一个Activity或Servi ...

随机推荐

  1. MVC3实现多个按钮提交

    有时我们需要在同一个Form表单中进行多个按钮的提交,来完成不同的功能.因为MVC中一个表单默认只能提交到一个Action处理,相对比较麻烦. 1.使用客户端脚本 我们可以view中加上如下代码: & ...

  2. 让一个div在不同的显示器中永远居中

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. 我的c++学习(7)引用和复制构造函数

    一.引用 什么是引用? 引用又称别名(alias),是一种非常特殊的数据类型.它不是定义一个新的变量,而是给一个已经定义的变量重新起一个别名,也就是 C++系统不为引用类型变量分配内存空间.引用主要用 ...

  4. [TJOI2016&HEOI2016]

    很有意思.是因为排序那道题才听闻今年tjoi2016的. 题是好题!先把它刷完再去把zhihu look through一遍. bzoj4552 以后看到什么做不出的题,看看能否写二分!!!!写二分! ...

  5. 键盘按钮keyCode大全,keyCode列表对照表

    目录 [显示] 1js获取键盘按键对应的keyCode 2字母和数字键的键码值 3数字键盘上的键的键码值和功能键键码值 4控制键键码值 js获取键盘按键对应的keyCode <!doctype ...

  6. Android内存进程管理机制

    参考文章: http://www.apkbus.com/android-104940-1-1.htmlhttp://blog.sina.com.cn/s/blog_3e3fcadd0100yjo2.h ...

  7. Python安装pandas

    http://blog.sina.com.cn/s/blog_a73687bc0101eenc.html 安装vcforpython: http://www.microsoft.com/en-us/d ...

  8. 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest A. Advanced 2048

    A. Advanced 2048 time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. js定时相关函数:

    定时相关函数: mytime= setTimeout(vCode, iMilliSeconds [, sLanguage]) -- 单次定时执行指定函数 clearTimeout(iTimeoutID ...

  10. phpstorm 10 修改背景图片和字体

    修改menu:File ~ Settings ~ Appearance & Behavior ~ Appearance ~ Theme 改成 Darcula即成黑色背景 menu字体大小: 编 ...