06 Android里的多线程及Handler通信

Android里的多线程编程与java里完全一样,无非要不就是继承Thread类要不就是实现接口Runnable的方式来实现多线程.

实现:点击窗口上的”newThread”按钮,创建一个子线程并分配线程的ID, 每个线程每隔3秒输出0 ~ 9.
界面布局:

/* activity_my.xml */

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jk.mythread.MyActivity">

    <Button
        android:id="@+id/btn_new"
        android:layout_width="208dp"
        android:layout_height="89dp"
        android:text="newThread"
        tools:layout_editor_absoluteX="81dp"
        tools:layout_editor_absoluteY="63dp" />

</android.support.constraint.ConstraintLayout>
/* MyActivity.java */
package com.example.jk.mythread;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends AppCompatActivity {
    private Button btn;
    private int threadId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        threadId = 0;

        btn = (Button) findViewById(R.id.btn_new);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyThread t = new MyThread(threadId++);
                t.start();
            }
        });


    }
}
/* MyThread.java */

package com.example.jk.mythread;

public class MyThread extends Thread {
    private int id;
    public MyThread(int id) {
        this.id = id;
    }

    public void run(){
        int i;

        for (i = 0; i < 10; i ++) {
            System.out.printf("thread id = %d : %d\n", id, i);
            try {
                Thread.sleep(3000); //休眠3秒
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}

编译执行程序后,每次点击按钮都会创建出一个子线程,打开”Android Monitor”就可以看到线程里的输出了.





线程与窗口间的消息通信可用android.os.Handler类对象来实现,而Handler对象只能实现单方向的通信(即一方只能接收消息,另一方只能发送消息). 基本用法:
1 消息接收方创建一个Handler对象,并实现它的handleMessage抽像函数:

        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
              //这里处理接收到的消息
            }
        };

2  当发送方通过handler发出消息后, handler的handleMessage函数会自动触发调用,处理接收到的消息.

3 发送方调用handler.obtainMessage(int what, Object obj).sendToTarget(); 产生一条消息并发送到接收方. 其中what参数用于区分消息的类型, 参数obj为各种类型的数据.



如实现线程通过Handler发出消息到窗口,窗口接收到消息后把内容输出到EditText对象里.
布局文件:

/* activity_my.xml */

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_new"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="newThread" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>
/* MyActivity.java */
package com.example.jk.mythread;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {
    private Button btn;
    private int threadId;
    private Handler handler;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        threadId = 0;

        editText = (EditText)findViewById(R.id.editText);
        btn = (Button) findViewById(R.id.btn_new);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyThread t = new MyThread(threadId++, handler);
                t.start();
            }
        });

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {

                editText.append("threadId " + msg.what + " : " + (String)msg.obj + "\n");

            }
        };

    }
}
/* MyThread.java */

package com.example.jk.mythread;

import android.os.Handler;

public class MyThread extends Thread {
    private int id;
    private Handler  mHandler;
    public MyThread(int id, Handler handler) {
        this.id = id;
        mHandler = handler;
    }

    public void run(){
        int i;

        for (i = 0; i < 10; i ++) {
            try {
                Thread.sleep(3000); //休眠3秒
            }catch (Exception e) {
                e.printStackTrace();
            }

            mHandler.obtainMessage(id, "num = "+i).sendToTarget();
        }
    }

}

编译执行程序后, 点击按钮创建线程后, 结果如下图:
这里写图片描述



如从窗口发送消息到线程,则在线程里创建Handler对象,实现接收消息. 这里需要注意真正属于线程的地方是run函数, 需要在run函数里创建Handler对象,这样才能创建出属于线程的Handler对象. 在线程类的构造函数创建的话也不是属于线程的,因构造函数是在创建线程对象被触发的,线程只有在调用start()后才会启动.

最后还需注意的地方, Handler对象是基于消息循环来接收消息,而一般的线程是不支持消息循环的,只能死循环。所以还需要实现支持消息循环的线程, 方法如下:

//创建一个线程,并实现支持消息循环
    new Thread() {
        public void run() { 
            Looper.prepare(); // 消息循环开始处
            uhandler = new Handler() {
                public void handleMessage(Message msg) {
                    //;
                }
            };
            Looper.loop();  //消息循环体到此为止
        }
    }.start();

窗口发送消息到线程的例子: 窗口把用户的输入内容通过Handler对象发送到线程里,线程里每隔3秒输出接收到内容.

/* activity_my.xml */
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="send" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />



</LinearLayout>
/* MyActivity.java */
package com.example.jk.mythread;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyActivity extends AppCompatActivity {
    private Button btn;
    private MyThread thread;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        thread = new MyThread();
        thread.start();

        editText = (EditText)findViewById(R.id.editText);
        btn = (Button) findViewById(R.id.btn_send);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            thread.handler.obtainMessage(0, editText.getText().toString()).sendToTarget();

            }
        });



    }
}
/* MyThread.java */
package com.example.jk.mythread;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class MyThread extends Thread {
    private String str;
    public Handler  handler;
    public MyThread() {
        str = "initial string";
    }

    public void run(){

    //创建一个专门处理循环接收消息的线程
        new Thread() {
            public void run() {
                Looper.prepare(); // 消息循环开始处
                handler = new Handler() {
                    public void handleMessage(Message msg) {
                        str = (String)msg.obj;
                    }
                };
                Looper.loop();  //消息循环体到此为止
            }
        }.start();


        //每隔3秒输出str的内容
        while (true) {
            Log.e("thread", str);
            try {
                Thread.sleep(3000);
            }catch (Exception e) {
                e.printStackTrace();
            }


        }

    }

}

Handler一个对象只能单向的通信,如需实现双向通信,则需要两个Handler对象才可以实现.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值