Sub Thread to update main Thread (UI)
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)的更多相关文章
- Sub Thread to update main Thread (UI) 2
Sub Thread to update main Thread (UI) 2 Handler.post(somethread); Handler.sendMessage("Msg&quo ...
- Does Daemon Thread Exit with Main Thread?
主线程(进程)退出后,主线程创建的守护线程也会退出吗? 通过下面的代码测试: Demo1: 进程创建普通线程 #!/usr/bin/python3 # FileName: daemonThread.p ...
- iOS开发,在main thread以外的thread更新UI
如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃. 例如,在异步block中去更改UI: NSOperationQueue *queue=[[NSOperation ...
- iOS Main Thread Checker: UI API called on a background thread的解释
Xcode打印栏出现如下警告: Main Thread Checker: UI API called on a background thread 这个是什么错误呢? 其实这并不一定是错误,也可以理解 ...
- 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 ...
- 13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)
今天花费了一天的时间来解决这个bug. 这种在程序运行期间出现的问题比较棘手,如果再没有规律的话就更难解决. 还好这个bug是由规律的,也就是说在程序执行半个小时左右后就会因为此异常而导致程序退出:那 ...
- 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 ...
- 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 ...
- 在Main Thread中使用异步
Whenever you first start an Android application, a thread called "main" is automatically c ...
随机推荐
- window下搭建Python3.7+selenium3.1.1+pycharm环境
1.安装Python3.7 1.1 下载 Python并安装 Python3.5 (勾选上 Add Python3.7 to PATH) 点击 Install Now,安装完成后将python路径加 ...
- 用Go语言写了一个电脑搜索文件的小东西
package main import ( "bytes" "fmt" "os" "os/exec" "pat ...
- javascript jquery 推断对象为空的方式
java中存在非常多空指针的问题,须要常常做预防和推断,如若不然,控制台出现恼人的异常,让人信心备受打击,早期敲代码的时候没有经验,不能依据异常信息找到问题的根源,唯一做的事情就是祈祷,千万别出现什么 ...
- php 数组元素高速去重
1.使用array_unique方法进行去重 对数组元素进行去重.我们通常会使用array_unique方法,使用这种方法能够把数组中的元素去重. <?php $arr = array(1,1, ...
- Crytek的幕后花絮
无论是哪种公司规模和状态.Xsolla都能够为其提供定制化的服务.我们提供定制化的技术集成,而不是提供一系列的解决方式.由于我们致力于满足每个合作伙伴的需求.整套的解决方式还存在着一系列的潜在隐患,我 ...
- 巧妇能为少米之炊(1)——Android下小内存下的生存之道
常常听到身边用安卓的朋友抱怨手机卡顿,内存动不动就快没了.而Google声称在512M的内存下也能流畅执行Android 4.4.究竟它做了什么? 总结一下它主要做了四件事: 1.优化内核,使用Act ...
- BZOJ 3503 高斯消元
思路: 高斯消元就好啦 注意每个格子最多只能和4个相邻 所以是 n*m*n*m*5 的 并不会TLE //By SiriusRen #include <cstdio> #include & ...
- BZOJ 1797 网络流的可行边&必须边
求完网络流以后 tarjan一发 判一判 //By SiriusRen #include <queue> #include <bitset> #include <cstd ...
- iOS菜鸟成长笔记(3)——斯坦福公开课学习(1)
一.iOS四层结构 1.Core OS 是用FreeBSD和Mach所改写的Darwin, 是开源.符合POSIX标准的一个Unix核心.这一层包含或者说是提供了整个iPhone OS的一些基础功能, ...
- Intellij使用"easyexplore"
刚开始接触Intellij,里面有很多东西还不太会用,平时在eclipse里面用的很方便的easyexplore能帮助快速打开文件目录,Intellij中本身就有这样的功能,只是默认没有开启,需要我们 ...