欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中子线程和UI线程通信详解

程序员文章站 2023-02-02 11:30:56
android中子线程和ui线程之间通信的详细解释 1.在多线程编程这块,我们经常要使用handler,thread和runnable这三个类,那么他们之间的关系你是否...

android中子线程和ui线程之间通信的详细解释

1.在多线程编程这块,我们经常要使用handler,thread和runnable这三个类,那么他们之间的关系你是否弄清楚了呢?下面详解一下。
2.首先在开发android应用时必须遵守单线程模型的原则:
android ui操作并不是线程安全的并且这些操作必须在ui线程中执行。
3.handler:
(1).概念:
handler是沟通activity 与thread/runnable的桥梁。而handler是运行在主ui线程中的,它与子线程可以通过message对象来传递数据。
(2).使用:
a:handler是运行在ui线程中,主要接收子线程发送的数据信息, 并用此数据配合主线程更新ui,用来跟ui主线程交互用。比如可以用handler发送一个message,然后在handler的线程中来接收、处理该消息。
b:消息的处理者。通过handler对象我们可以封装message对象,然后通过sendmessage(msg)把message对象添加到messagequeue中;当messagequeue循环到该message时,就会调用该message对象对应的handler对象的handlemessage()方法对其进行处理。
c:handler可以分发runnable对象,也可以分发message对象。

4.message:
 消息对象,顾名思义就是记录消息信息的类。也就是说是信息的载体,存放信息内容。这个类有几个比较重要的字段:

  (1).arg1和arg2:我们可以使用两个字段用来存放我们需要传递的整型值,在service中,我们可以用来存放service的id。
  (2).obj:该字段是object类型,我们可以让该字段传递某个对象到消息的接受者中。
  (3).what:这个字段可以说是消息的标志,判断是接收了哪个消息。在消息处理中,我们可以根据这个字段的不同的值进行不同的处理,类似于我们在处理button事件时,通过switch(v.getid())判断是点击了哪个按钮。
android推荐通过message.obtain()或者handler.obtainmessage()获取message对象。这并不一定是直接创建一个新的实例,而是先从消息池中看有没有可用的message实例,存在则直接取出并返回这个实例。反之如果消息池中没有可用的message实例,则根据给定的参数new一个新message对象。通过分析源码可得知,android系统默认情况下在消息池中实例化10个message对象。
5.源码展示:
(1).activity_main.xml布局文件:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="自定义thread继承thread" />


  <button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="自定义runnable实现runnable" />


  <button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="定时更新ui界面,handler分发runnable对象" />


  <button
    android:id="@+id/btn4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="定时更新ui界面,handler分发message对象" />

  <textview
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0" />

</linearlayout>

(2).mainactivity.java

package com.chengdong.su.threaddemo;

import com.chengdong.su.threaddemo.util.myrunnable;
import com.chengdong.su.threaddemo.util.mythread;

import android.r.integer;
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.view.view.onclicklistener;
import android.widget.button;
import android.widget.textview;

public class mainactivity extends activity implements onclicklistener {
  /** tag */
  private final string tag = getclass().getsimplename();
  /** the object of the button */
  private button mbutton;
  /** the object of the button */
  private button mbutton2;
  /** the object of the button */
  private button mbutton3;
  /** the object of the button */
  private button mbutton4;
  /** the object of the textview */
  private textview mtextview;
  /** 计数 */
  private int mcount = 0;
  /** 标志 */
  private int message_flag = 1;

  /**
   * handler分发runnable对象的方式
   */
  private handler mhandler = new handler();

  runnable runnable = new runnable() {

    @override
    public void run() {
      mcount++;
      mhandler.postdelayed(runnable, 1000);
      mtextview.settext(mcount + "");

    }
  };
  /***
   * handler分发message对象的方式
   */

  handler mhandler2 = new handler() {
    public void handlemessage(android.os.message msg) {
      if (msg.what == 1) {

        mtextview.settext("handler分发message对象的方式");
      }
    }
  };

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    initview();

  }

  /**
   * 初始化组件对象
   */
  private void initview() {
    mbutton = (button) findviewbyid(r.id.btn);
    mbutton2 = (button) findviewbyid(r.id.btn2);
    mbutton3 = (button) findviewbyid(r.id.btn3);
    mbutton4 = (button) findviewbyid(r.id.btn4);

    mbutton.setonclicklistener(this);
    mbutton2.setonclicklistener(this);
    mbutton3.setonclicklistener(this);
    mbutton4.setonclicklistener(this);

    mtextview = (textview) findviewbyid(r.id.tv);

  }

  @override
  public void onclick(view v) {
    switch (v.getid()) {
    case r.id.btn: {
      // 方法一:继承的方式:自定义thread继承thread,开启一个新的线程
      new mythread().start();
      break;
    }
    case r.id.btn2: {
      // 方法二:实现的方式:implement runnable
      new thread(new myrunnable()).start();
      break;
    }
    // 方法三:handler分发runnable对象:定时更新ui界面 提交计划任务马上执行
    case r.id.btn3: {

      // handler分发runnable对象
      mhandler.post(runnable);
      break;
    }
    // 方法四:handler分发message对象 ,定时更新ui界面 提交计划任务马上执行
    case r.id.btn4: {

      // 不推荐这种方式
      // message msg = new message();
      // 推荐使用这种获取对象的方式:从消息池中获得可用的message对象
      message msg = message.obtain();
      msg.what = message_flag;
      mhandler2.sendmessage(msg);
      break;
    }

    default:
      break;
    }

  }
}

(3).myrunnable.java

package com.chengdong.su.threaddemo.util;

import android.util.log;

/***
 * 自定义一个myrunnable线程
 * 
 * @author scd
 * 
 */
public class myrunnable implements runnable {

  public myrunnable() {
    super();
  }

  /** tag */
  private final string tag = getclass().getsimplename();

  @override
  public void run() {
    for (int i = 0; i < 20; i++) {
      log.e(tag, thread.currentthread().getname() + ",实现的方法" + i);

    }

  }

}

(4)mythread.java

package com.chengdong.su.threaddemo.util;

import android.util.log;

/***
 * 自定义一个线程
 * 
 * @author scd
 * 
 */
public class mythread extends thread {

  public mythread() {
    super();
  }

  /** tag */
  private final string tag = getclass().getsimplename();

  @override
  public void run() {
    super.run();
    for (int i = 0; i < 10; i++) {
      log.e(tag, thread.currentthread().getname() + ",继承thread类:" + i);

    }
  }

}