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. (52)KeyError错误

    当出现:22KeyError: 'xxxxxx'这样的错误,可以肯定依赖出问题了,一般是你引用某个字段,但没有定义,又没有指定依赖的模块,最后找不到对应字段 解决方法:开发工具,把 xxxxxx 去搜 ...

  2. caioj 1157 线性筛选素数

    注意这道题开得非常大,有2*1e7 自己可以养成一种习惯,如果数据是很容易的话,可以自己手动输入极限数据来测试自己的程序 #include<cstdio> #include<algo ...

  3. readb(), readw(), readl(),writeb(), writew(), writel() 宏函数

    参见: http://blog.csdn.net/hustyangju/article/details/20448339

  4. nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONObject异常的解决办法

    解决办法:你可以尝试添加一个jar包,因为我加入了一个jar包后错误问题成功解决. 将所需要的jettison-1.2.jar包复制到lib文件夹里面,重启项目,问题搞定.

  5. android:Activity启动模式之singleTask(一)

    先看一下standard启动模式的说明: 仅仅有一个实例.在同一个应用程序中启动他的时候.若不存在此Activity实例.则会在当前栈顶创建一个新的实例.若存在,则会把栈中在其上的其他Activity ...

  6. Unity Image Effect

    1.工作原理 Image Effect是运用于一个Camera,将Camera视见体最终看到的二维图像内容作为一个2DTexture传递给一个Shader, 然后在Shader的Fragment渲染阶 ...

  7. poj--3620--Avoid The Lakes(dfs)

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  8. spring boot 的常用注解使用 总结

    附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...

  9. vuejs fatherandson

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 运维派 企业面试题2 创建10个 "十个随机字母_test.html" 文件

    Linux运维必会的实战编程笔试题(19题) 企业面试题2: 使用for循环在/tmp/www目录下通过随机小写10个字母加固定字符串test批量创建10个html文件,名称例如为: --[root@ ...