Handler的正确使用,软引用封装

Posted 王永迪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Handler的正确使用,软引用封装相关的知识,希望对你有一定的参考价值。

package com.xiaobai.mizar.utils;

import android.os.Handler;
import android.os.Message;
import com.base.platform.utils.android.Logger;
import java.lang.ref.WeakReference;

/**
 * Author: walid
 * Date : 2015/10/12 18:40
 */
public class TaskHandler<T> extends Handler 

	WeakReference<T> weakReference;

	public TaskHandler(T object) 
		weakReference = new WeakReference<>(object);
	

	/**
	 * 任务执行成功
	 */
	public static final int TASK_OK = 0x10000001;
	/**
	 * 任务执行失败
	 */
	public static final int TASK_FAILED = 0x10000002;
	/**
	 * 任务取消执行
	 */
	public static final int TASK_CANCELED = 0x10000003;

	/**
	 * 消息接受处理
	 */
	@Override
	public void handleMessage(Message msg) 
		T object = weakReference.get();
		if (object != null) 
			switch (msg.what) 
				case TASK_OK:
					onTaskOk(object, msg);
					break;
				case TASK_FAILED:
					onTaskFailed(object, msg);
					break;
				case TASK_CANCELED:
					onTaskCanceled(object, msg);
					break;
			
		
	


	/**
	 * 发送带消息的失败消息
	 */
	public void sendFailedMessage(Object object) 
		Logger.d("Send Failed Message");
		Message msg = Message.obtain();
		msg.what = TASK_FAILED;
		msg.arg1 = 0;
		if (null != object) 
			msg.obj = object;
		
		this.sendMessage(msg);
	

	/**
	 * 发送带消息的失败消息
	 */
	public void sendFailedMessage(Object object, int arg1) 
		Logger.d("Send Failed Message");
		Message msg = Message.obtain();
		msg.what = TASK_FAILED;
		msg.arg1 = arg1;
		if (null != object) 
			msg.obj = object;
		
		this.sendMessage(msg);
	

	/**
	 * 发送带延时消息的失败消息
	 */
	public void sendFailedDelayedMessage(Object object, int arg1,
	                                     long timedelay) 
		Logger.d("Send Failed Message");
		Message msg = Message.obtain();
		msg.what = TASK_FAILED;
		msg.arg1 = arg1;
		if (null != object) 
			msg.obj = object;
		
		this.sendMessageDelayed(msg, timedelay);
	

	public void sendEmptyFailedMessage() 
		Logger.d("Send Failed Message");
		this.sendEmptyMessage(TASK_FAILED);
	

	// 发送带消息的成功消息
	public void sendSucessMessage(Object object) 
		Logger.d("Send Sucess Message");
		Message msg = Message.obtain();
		msg.what = TASK_OK;
		msg.arg1 = 0;
		if (null != object) 
			msg.obj = object;
		
		this.sendMessage(msg);
	

	// 发送带消息的成功消息
	public void sendSucessMessage(Object object, int arg1) 
		Logger.d("Send Sucess Message");
		Message msg = Message.obtain();
		msg.what = TASK_OK;
		msg.arg1 = arg1;
		if (null != object) 
			msg.obj = object;
		
		this.sendMessage(msg);
	

	public void sendEmptySucessMessage() 
		Logger.d("Send Sucess Message");
		this.sendEmptyMessage(TASK_OK);
	

	public void sendEmptyTaskCancelMessage() 
		Logger.d("Send canceled Message");
		this.sendEmptyMessage(TASK_CANCELED);
	

	/**
	 * 当消息类型为TASK_OK时,回调该方法 可以通过arg1进行消息标记
	 */
	public void onTaskOk(T object, Message msg) 

	

	/**
	 * 当消息类型为TASK_FAILED时,回调该方法 可以通过arg1进行消息标记
	 */
	public void onTaskFailed(T object, Message msg) 

	

	/**
	 * 当消息类型为TASK_CANCELED时,回调该方法处理发送的对象数据 可以通过arg1进行消息标记
	 */
	public void onTaskCanceled(T object, Message msg) 

	
<pre name="code" class="javascript">        调用方式:
	static class LoadMoreHandler extends TaskHandler<HeaderableAdapter> 

		public LoadMoreHandler(HeaderableAdapter headerableAdapter) 
			super(headerableAdapter);
		

		@Override
		public void onTaskOk(HeaderableAdapter headerableAdapter, Message msg) 
			super.onTaskOk(headerableAdapter, msg);
			headerableAdapter.tvLoadMore.setClickable(true);
			headerableAdapter.tvLoadMore.setText(headerableAdapter.strClickLoadMore);
			headerableAdapter.tvLoadMore.setTextColor(headerableAdapter.mainColor);
			headerableAdapter.tvLoadMore.setTextSize(16);
		
	


 使用调用:
new LoadMoreHandler(this).sendEmptyMessageDelayed(TaskHandler.TASK_OK, 3000);


开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于Handler的正确使用,软引用封装的主要内容,如果未能解决你的问题,请参考以下文章

正确理解为什么 Handler 会导致内存泄露?

Java基础 - 引用类型

如何通过软引用和弱引用提升JVM内存使用效率?

正确使用 beginBackgroundTaskWithExpirationHandler

强引用软引用弱引用虚引用以及他们之间和 gc 的关系

Android性能提升之强引用软引用弱引用虚引用使用