• Android:子线程向UI主线程发送消息


    在Android里,UI线程是不同意被堵塞的。因此我们要将耗时的工作放到子线程中去处理。
    那么子线程耗时处理后要如何通知UI线程呢?

    我们能够在UI主线程中创建一个handler对象,然后通过重写其handleMessage(Message msg)的方法,该方法会接收到子线程中的handler对象的sendMessage((Message msg)发回来的消息。这样一发一收就完毕工作。

    而关于主线程向子线程发送消息的内容能够看我的上一篇博客。当中讲到了Looper类及其两个重要方法和实现原理。

    在Android中当UI主线程本身已实现了Looper的功能。所以不用我们担心。所以子线程向主线程发送消息是很easy的。

    以下写个样例。子线程每秒钟会向UI主线程发送一个数字。UI主线程会将该数字显示在自己的TextView控件上。

    好那么先来看一下执行截图吧:

    这里写图片描写叙述

    以下附上代码:

    MainActivity.java:

    package activity.wyc.com.threaddemo;
    
    import android.os.Handler;
    import android.os.Message;
    import android.os.SystemClock;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    
    
    public class MainActivity extends ActionBarActivity {
    
        private TextView tvObj;
    
        private Handler handler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tvObj = (TextView) findViewById(R.id.tvid);
    
            handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
    
                    tvObj.setText(String.valueOf(msg.arg1));
    
                }
            };
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int num = 0;
    
                    while (true) {
                        Message message = Message.obtain();
                        message.arg1 = num;
    
                        handler.sendMessage(message);
    
                        SystemClock.sleep(1000);
                        num = ++num;
                    }
                }
            }).start();
    
        }
    
    
    }
    

    activity_main.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:gravity="center_horizontal"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/tvid"
            android:textSize="40sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    以下是源代码(百度云盘。Android studio编写):

    http://pan.baidu.com/s/1kT9oKbP

  • 相关阅读:
    React 生命周期
    css 多行文本以...代替
    微信JSSDK配置文件说明
    zepto阻止事件冒泡
    PHP 图片处理PNG颜色丢失
    React 学习笔记(一)
    webpack webpack-dev-server使用指南
    为什么需要使用模块打包工具?
    如何实现微信公户绑定公众号业务
    iOS 手势
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7349967.html
Copyright © 2020-2023  润新知