常用的两种handler调用方法

一、Handler、Thread、HandlerThread三者之间的关系如下:

1、Handler:在android中负责发送和处理消息,通过它可以实现其他支线线程与主线程之间的消息通讯。

2、Thread:Java进程中执行运算的最小单位,亦即执行处理机调度的基本单位。某一进程中一路单独运行的程序。

3、HandlerThread:一个继承自Thread的类HandlerThread。
二、Handler跟Thread结合使用,首先在FirstHandler.java类中创建要在界面调用的getSum方法,具体实现如下:
 

package com.example.handlerdemo;

import android.os.Handler;
import android.os.Message;

public class FirstHandler {

	private Handler mHandler;
	public static final int ADD_FLAG = 1;
	public void setHandler(Handler handler){
		mHandler = handler;
	}
	
	public synchronized void getSum(final int a, final int b){
		Logger.d("a = " + a + " ,b = " + b);
		class AddRunThread implements Runnable{

			@Override
			public void run() {
				Logger.d("FirstHandler ThreadId = " + Thread.currentThread().getId());
				int sum = add(a, b); 
				try {
					Thread.sleep(6 * 1000); //模拟加法运算是一个非常复杂的运算,所以要开线程
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				String result = "result = " + sum;
				Message message = mHandler.obtainMessage();
				message.what = ADD_FLAG;
				message.obj = result;
				mHandler.sendMessage(message); //在新的线程中进行发送Message消息
			}
		}
		Thread addRun = new Thread(new AddRunThread());
		addRun.setPriority(Thread.MAX_PRIORITY); //设置线程优先级
		addRun.start();//线程启动
	}
	/**
	 * 实现加法运算
	 */
	private int add(int a, int b){
		return a + b;
	}
}

当调用 getSum方法的时候会先启动addRun线程,addRun线程启动后就会执行AddRunThread类里面的run方法,run方法里面执行了一个加法运算的add方法,然后会执行Thread.sleep(6 * 1000)来模拟耗时的操作。加法运算add方法实行结束后会通过mHandler.sendMessage(message)来通知界面更新信息。接下来看一下界面MainActivity的实现,具体如下:

package com.example.handlerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {
	private TextView textView;
	private FirstHandler firstHandler;
	private SecondHandler secondHandler;
	private String result;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Logger.d("Handler ThreadId = " + Thread.currentThread().getId());
			switch (msg.what) {
			case FirstHandler.ADD_FLAG:
				Logger.d("FirstHandler.ADD_FLAG");
				textView.setText((String) msg.obj); //更新加法运算后的结果
				break;
			case SecondHandler.UI_REFLESH:
				Logger.d("SecondHandler.UI_REFLESH"); 
				textView.setText((String) msg.obj); //更新减法运算后的结果
				break;
			default:
				break;
			}
		};
	};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.d("main ThreadId = " + Thread.currentThread().getId());
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tx_show);
        firstHandler = new FirstHandler();
        secondHandler = new SecondHandler();
    }

    /*
     * handler跟Thread结合使用的测试方法
     */
    public void addCheck(View view){
    	Logger.d();
    	firstHandler.setHandler(handler); //设置Handler对象实例
    	firstHandler.getSum(3000, 4600);  //调用FirstHandler类的getSum方法
    }
    
    /*
     * handler跟HandlerThread结合使用的测试方法
     */
    public void decreaseClick(View view){
    	Logger.d();
    	secondHandler.getSerialThread().post(new Runnable() {
			
			@Override
			public void run() {
				Logger.d("SecondHandler ThreadId = " + Thread.currentThread().getId());
				int sum = decrease(300, 180);
				try {
					Thread.sleep(8 * 1000); //模拟减法运算是一个非常复杂的运算,所以要开线程
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				result = "result2 = " + sum;
				Message message = new Message();
				message.what = SecondHandler.UI_REFLESH;
				message.obj = result;
				handler.sendMessage(message); //通过主线程更新UI
			}
		});
    }
    
    //减法运算
    private int decrease(int a, int b){
    	return a - b;
    }
    
}

 可以看到在handleMessage中进行了消息处理,当msg.what等于FirstHandler.ADD_FLAG时,就会将加法运算的结果设置到textView中展示。

常用的两种handler调用方法_第1张图片 

 

三、handler跟HandlerThread结合使用。代码逻辑主要在SecondHandler.java类中实现,具体如下:

package com.example.handlerdemo;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

public class SecondHandler {
	
	public Handler uiHandler = new Handler(Looper.getMainLooper()); //main主线程,由于更新UI更新
	private HandlerThread mHandlerThread;
	private Handler handler;
	public static final int UI_REFLESH = 0X02;
	
	//设置handler对象关联
	public void setUIhandler(Handler handler){
		uiHandler = handler;
	}
	
	/**
	 *获取获得Looper对象的Handler实例
	 */
	public synchronized Handler getSerialThread(){
		if (null == mHandlerThread){
			HandlerThread thread = new HandlerThread("work_thread");//工作线程,用于耗时的操作,不能用于更新UI
			thread.start();
			handler = new Handler(thread.getLooper()){
			};
		}
		return handler;
	}
}

通过 new Handler(thread.getLooper())来建立handler跟HandlerThread的联系。接下来看一下具体怎么调用getSerialThread()方法,如下:

 /*
     * handler跟HandlerThread结合使用的测试方法
     */
    public void decreaseClick(View view){
    	Logger.d();
    	secondHandler.getSerialThread().post(new Runnable() {
			
			@Override
			public void run() {
				Logger.d("SecondHandler ThreadId = " + Thread.currentThread().getId());
				int sum = decrease(300, 180);
				try {
					Thread.sleep(8 * 1000); //模拟减法运算是一个非常复杂的运算,所以要开线程
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				result = "result2 = " + sum;
				Message message = new Message();
				message.what = SecondHandler.UI_REFLESH;
				message.obj = result;
				handler.sendMessage(message); //通过主线程更新UI
			}
		});
    }
    
    //减法运算
    private int decrease(int a, int b){
    	return a - b;
    }

首先通过 getSerialThread()方法获取secondHandler对象,然后调用了post方法,在run方法中执行了减法decrease(300, 180)运算,减法运算结束后会调用一个休眠函数Thread.sleep(8 * 1000),休眠8秒来模拟是耗时操作,休眠结束后会通过主线程的handler来通知界面更新减法的结果。运行结果如下:
常用的两种handler调用方法_第2张图片

 代码参考:https://github.com/gunder1129/android-tool/commit/219738d73f22763ab531800e423a66c8d3e72860

参考博客:https://blog.csdn.net/weixin_41101173/article/details/79687313

 

 

你可能感兴趣的:(android)