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.进程和线程的定义 进程并不只是一段可以运行的代码,也包含了运行代码所需要的资源. 在操作系统来看,进程是资源管理的最小单元,而我们又知道,线程是程序执行的最小单元. 话说回来 ...

  2. SQL 字符串操作函数

    SQL 字符串操作函数 学习了: https://www.cnblogs.com/wangzhe688/p/6046654.html 一.字符转换函数 1.ASCII() 返回字符表达式最左端字符的A ...

  3. 使用sh运行bash脚本的奇怪问题

    在同一个文件夹下有两个脚本.a.sh和b.sh,脚本内容例如以下: a.sh: echo "test for a" source b.sh b.sh: echo "tes ...

  4. 2015百度之星初赛2 1005 序列变换(LIS变形)

    LIS(非严格):首先我想到了LIS.然而总认为有点不正确:每一个数先减去它的下标.防止以下的情况发生:(转载) 3 增加序列是1,2,2,2,3,这样求上升子序列是3.也就是要改动2个,可是中间的两 ...

  5. SVN配置以及自己主动部署到apache虚拟文件夹

    SVN配置以及自己主动部署到apache虚拟文件夹 一.VisualSVN server 服务端和TortoiseSVNclient下载 VisualSVN下载:http://subversion.a ...

  6. 物理读之LRU(近期最少被使用)的深入解析

    转载请注明出处: http://blog.csdn.net/guoyjoe/article/details/38264883 一组LRU链表包含LRU主链.LRU辅助链.LRUW主链,LRUW辅助链, ...

  7. 6、python中的字符串

    最早的编码为ascii码,共256个符号.UTF-8是国际通用编码,全面支持中文,以一个字节表示英文,以三个字节表示一个中文以及其他语言:GB2312是我国自己定制的中文编码标准,使用1个字节表示英文 ...

  8. RvmTranslator for Linux

    RvmTranslator for Linuxeryar@163.com RvmTranslator can translate the RVM file exported by AVEVA Plan ...

  9. nyoj-673-悟空的难题(数组标记)

    悟空的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 自从悟空当上了齐天大圣.花果山上的猴子猴孙们便也能够尝到天上的各种仙果神酒,所以猴子猴孙们的体质也得到了非 ...

  10. vim 插件之NERD tree

    NERD tree 这个插件可以用来快速浏览目录结构,打开文件 地址 http://www.vim.org/scripts/script.php?script_id=1658 https://gith ...