Sub Thread to update main Thread (UI)  2

Handler.post(somethread);

Handler.sendMessage("Msg");

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
 
public class HandlerActivity extends Activity {
     
    Button btnStart,btnEnd;
    ProgressBar proBar;
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        //通过控件的ID来实例化控件对象
         btnStart = (Button)findViewById(R.id.start);
        btnEnd = (Button)findViewById(R.id.end);
        proBar = (ProgressBar)findViewById(R.id.pBar);
         
        //开始按钮触发事件
        btnStart.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                proBar.setVisibility(View.VISIBLE);
                <span style="color: #ff0000;">updateBarHandler.post(updateBarThread);</span>
            }
        });
         
        //结束按钮触发事件
        btnEnd.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View v) {
                <span style="color: #ff0000;">updateBarHandler.removeCallbacks(updateBarThread);</span>
            }
        });
    }
     
     
     //创建一个Handler对象
     Handler updateBarHandler = new Handler(){
 
        @Override
        public void handleMessage(Message msg) {
            proBar.setProgress(msg.arg1); //REPEAT
             updateBarHandler.post(updateBarThread);
        }
         
    };
     
    //更新ProgressBar的线程对象
    Runnable updateBarThread = new Runnable() {
        int i = 0;
        @Override
        public void run() {
            i = i + 10;
            Message msg = updateBarHandler.obtainMessage();
            msg.arg1 = i;
            try{
                Thread.sleep(2000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            updateBarHandler.sendMessage(msg);
            if(i == 100){
                updateBarHandler.removeCallbacks(updateBarThread);
            }
        }
    };
}

解释:

使用Handler的大致流程:

1.首先创建一个Handler对象,可以直接使用Handler无参构造函数创建Handler对象,也可以继承Hander类,重写HandleMessage方法来创建Handler对象

2.在监听器中,调用Handler的post方法,将要执行的线程对象加到线程队列当中。此时将会把线程对象添加到handler对象的线程队列中

3.将要执行的操作写在线程对象的run方法中,一般一个Runnable对象,复写其中的run方法就可以了。

Handler包含了两个队列,其中一个是线程队列,另外一个是消息队列。使用post方法会将线程对象放到该handler的线程队列中,使用sendMessage将消息放到消息队列中。

如果想要在这个流程一直执行的话,可以在run方法内部执行postDelayed或者post方法,再将该线程对象添加到消息队列中,重复执行。想要线程停止执行,调用Handler对象的removeCallbacks方法,从线程队列中移除线程对象,是线程停止执行。

Handler为Android提供了一种异步消息处理机制,当向消息队列中发送消息(sendMessage)后就立即返回,而从消息队列中读取消息时会阻塞,其中消息队列中读取消息时会执行Handler中的public void handleMessage方法,因此在创建Handler时,应该使用匿名内部类重写该方法,在该方法中写上读取到消息后的操作,使用Handler的obtainMessage()来获得消息的对象。

Handler与线程的关系:

使用Handler的post方法将Runable对象放到Handler的线程队列中后,该Runnalbe的执行其实并未单独开启线程,而是仍然在当前Activity线程中执行的,Handler只是调用了Runable对象的run方法。

如何让Handler执行Runnable时打开新的线程:

1.首先生成一个HandlerThread对象,实现了使用Looper来处理消息队列的功能,这个类由Android应用程序架构提供。

HandlerThread handlerThread=new HandlerThread(“handler_thread”);

2.在使用HandlerThread的getLooper()方法之前,必须先调用该类的start();

3.根据这个HandlerThread对象得到其中的Looper对象。

4.创建自定义的继承于Handler类的子类,其中实现一个参数为Looper对象的构造方法,方法内容调用父类的构造函数即可。

5.使用第三步得到的Looper对象创建自定义的Handler子类的对象,再将消息发送到该Handler的消息队列中,Handler复写的handleMessage()将会执行来处理消息队列中的消息。

消息,即Message对象,可以传递一些信息,可以使用arg1,arg2,Object传递一些整形或者对象,还可以使用Message对象的setData(Bundle bundle)来将Bundle对象传递给新创建的线程,新创建的线程在执行handleMessage时可以从message中利用getData()提取出Bundle对象进行处理。

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

  1. Sub Thread to update main Thread (UI)

    Sub Thread to update main Thread (UI) main Thread :   A  has Hander.HandleMessage() to process the & ...

  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. caioj 1161 欧拉函数3:可见点数

    (x, y)被看到仅当x与y互质 由此联想到欧拉函数 x=y是1个点,然后把正方形分成两半,一边是φ(n) 所以答案是2*∑φ(n)+1 #include<cstdio> #include ...

  2. Qt之图形(简笔画-绘制卡通蚂蚁)

    简述 关于简笔画的介绍很多,有动物.水果.蔬菜.交通工具等,通常会对绘制一步步进行拆分.组合.然后绘制为我们想要的结果. 下面来介绍另外的一个种类:昆虫类-卡通蚂蚁. 简述 绘制 效果 源码 绘制 效 ...

  3. jquery-ui日期时间控件实现

    日期控件和时间控件为独立控件,日期时间控件要同一时候导入日期控件和时间控件的js,然后在日期控件加入时间控件显示參数,没有导入时间控件js.日期控件函数设置的时间控件參将包错 日期控件官网网址:htt ...

  4. iOS设计模式之NSNotificationCenter 消息中心

    消息中心模式和KVO模式有点相似,差别在于.KVO  模式是意图在于监听摸一个相应的值的变化.而去出发一个方法相应的动作.而消息中心在于,广播.它就像一个广播基站,发送一条消息,在全部的加入监听的地方 ...

  5. Invalid property 'sentinels' of bean class redis spring 错误修改

    /* * Copyright 2014-2015 the original author or authors. * * Licensed under the Apache License, Vers ...

  6. 一天一个算法:求Sn=a+aa+aaa+…+aa…a之和

    /* 求Sn=a+aa+aaa+…+aa…a之值,其中a是一个数字. 例如:2+22+222+…+22222(此时n=5),n由键盘输入.*/ void Function3() { int a,n,s ...

  7. Flume 启动

    Configuration是Flume项目的入口程序了,当我们输入 bin/flume-ng agent --conf conf --conf-file conf/kafka1.properties ...

  8. Orientdb基本操作

    https://blog.csdn.net/clj198606061111/article/details/82314459

  9. appium 模拟实现物理按键点击

    appium自动化测试中,当确认,搜索,返回等按键通过定位点击不好实现的时候,可以借助物理按键来实现.appium支持以下物理按键模拟: 电话键 KEYCODE_CALL 拨号键 5 KEYCODE_ ...

  10. docker切换默认镜像源

    docker切换默认镜像源   基于 debian8 默认安装的 docker 镜像源是在国外,pull 镜像的时候奇慢无比,需要自己手动切换成国内的镜像源. 1. 修改配置文件 docker 默认的 ...