Sub Thread to update main Thread (UI)

main Thread :   A  has Hander.HandleMessage() to process the "Msg" from subthread B;

Sub Thread :    B  use  Hander.sendMessage(Msg)  to main Thread A;

 import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class HandlerDemo extends Activity { //title为setTitle方法提供变量,这里为了方便我设置成了int型
private int title = 0;
  private Handler mHandler = new Handler(){ 
        public void handleMessage(Message msg) { 
            switch (msg.what) { 
            case 1: 
                updateTitle(); 
                break; 
            } 
        }; 
    };
  public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
  
        Timer timer = new Timer(); 
      timer.scheduleAtFixedRate(new MyTask(), 1, 5000);  
    } 
  
    private class MyTask extends TimerTask{ 
        @Override 
        public void run() { 
     //处理事情
            Message message = new Message(); 
            message.what = 1; 
            mHandler.sendMessage(message);
  
        }    
    } 
  
  
    public void updateTitle(){ 
  
        setTitle("Welcome to Mr Wei's blog " + title); 
        title ++; 
    } 
}

Sub Thread to update main Thread (UI)的更多相关文章

  1. Sub Thread to update main Thread (UI) 2

    Sub Thread to update main Thread (UI)  2 Handler.post(somethread); Handler.sendMessage("Msg&quo ...

  2. Does Daemon Thread Exit with Main Thread?

    主线程(进程)退出后,主线程创建的守护线程也会退出吗? 通过下面的代码测试: Demo1: 进程创建普通线程 #!/usr/bin/python3 # FileName: daemonThread.p ...

  3. iOS开发,在main thread以外的thread更新UI

    如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃. 例如,在异步block中去更改UI: NSOperationQueue *queue=[[NSOperation ...

  4. iOS Main Thread Checker: UI API called on a background thread的解释

    Xcode打印栏出现如下警告: Main Thread Checker: UI API called on a background thread 这个是什么错误呢? 其实这并不一定是错误,也可以理解 ...

  5. iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...

  6. 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)

    今天花费了一天的时间来解决这个bug. 这种在程序运行期间出现的问题比较棘手,如果再没有规律的话就更难解决. 还好这个bug是由规律的,也就是说在程序执行半个小时左右后就会因为此异常而导致程序退出:那 ...

  7. reloadData should be in main thread

    reloadData should be called in main thread, so if you call it in work thread, you should call it as ...

  8. APP崩溃提示:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    崩溃输出日志 2017-08-29 14:53:47.332368+0800 HuiDaiKe[2373:1135604] This application is modifying the auto ...

  9. 在Main Thread中使用异步

    Whenever you first start an Android application, a thread called "main" is automatically c ...

随机推荐

  1. Linux入门基础二

    1.查看用户 who参数          说明 -a      打印能打印的全部 -d      打印死掉的进程 -m    同am i,mom likes -q      打印当前登录用户数及用户 ...

  2. 为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性

    为什么button在设置标题时要用一个方法.而不像lable一样直接用一个属性 原因是有时我们对      button做一次点击,须要改变button的标题.仅仅实用方法才干做到,而label是标签 ...

  3. 转一篇关于vuex简单理解的文章

    学习vuex半天摸不着头脑无意间发现了这篇文章 对vuex做了一个简单的阐述比较有助于我的理解 现在分享出来希望能给一些朋友一点帮助  这个是原文地址 http://www.ituring.com.c ...

  4. nj04---事件回调函数

    一.回调函数 1.异步式读取文件 var fs=require('fs'); fs.readFile('file.txt','utf-8',function(err,data){ if(err){ c ...

  5. BZOJ4477: [Jsoi2015]字符串树

    [传送门:BZOJ4477] 简要题意: 给出一棵n个点的树,树上的边都代表一个字符串,给出Q个询问,每个询问输入x,y和字符串s,求出x到y的路径上以s为前缀的字符串个数 题解: 自己yy了一波可持 ...

  6. 转一篇100offer的采访~35岁程序员是一种什么状态

    随着互联网的高速发展变革,大龄恐惧症越来越多地在技术圈被人讨论.很多程序员在工作5-10年以后,都会开始思考5年.10年甚至更久以后的自己,会是怎样一种生活工作状态,以及是否会被时代抛弃. 特别是全民 ...

  7. jquery操作select的各种方法

    在工作中,有时候会遇到给select组件添加一些事件,前两天发表了一篇文章,<用jquery给select加选中事件>大致阐述了简单的jq操作select的方法,但是为了详细的介绍一下se ...

  8. mysql语句判断一天操作记录的个数

    话说有一文章表article,存储文章的添加文章的时间是add_time字段,该字段为int(5)类型的,现需要查询今天添加的文章总数并且按照时间从大到小排序,则查询语句如下: 1    select ...

  9. 什么是Monad?

    为了理解什么是Monad,最好需要了解什么是Monoid.这两篇互为姐妹篇,因为Monad的定义是:A monad is just a monoid in the category of endofu ...

  10. python(2) 图像通道,几何变换,裁剪

    一.图像通道 1.彩色图像转灰度图 from PIL import Image import matplotlib.pyplot as plt img=Image.open('d:/ex.jpg') ...