Android在其他线程中更新UI

public class TransferTools {

	private static final int MSG_START = 1001;
	private static final int MSG_TRANSFERRING = 1002;

	public void setTransferListener(TransferListener mListner) {
		mTransferListener = mListner;
	}

	public interface TransferListener {
		/**
		 * 开始
		 */
		void onStart();

		void onTransfer(String text);
	}

	private static TransferListener mTransferListener = null;
	
	private static Handler sHandler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(android.os.Message msg) {
			if (mTransferListener == null) {
				return;
			}
			switch (msg.what) {

			case MSG_START:
				mTransferListener.onStart();
				break;
			case MSG_TRANSFERRING:
				mTransferListener.onTransfer((String) msg.obj);
				break;

			default:
				break;
			}

		};
	};

	private void work() {
		//...耗时操作,如网络请求
		Message msg = sHandler.obtainMessage();
		msg.what = MSG_START;
		msg.obj = message;
		sHandler.sendMessage(msg);
	}
}

  

posted @ 2015-10-16 18:23  jasonkent27  阅读(246)  评论(0编辑  收藏  举报