Handler操作记录 Only one Looper may be created per thread

public class MainActivity extends Activity {

private Button caculateButton;
private EditText input;
private TextView output;
private ProgressBar progressBar;
private CaculateThread cacThread;
private Handler mHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	caculateButton = (Button) findViewById(R.id.caculate);
	progressBar = (ProgressBar) findViewById(R.id.process);
	input = (EditText) findViewById(R.id.inputView);
	output = (TextView) findViewById(R.id.outputView);
    // 点击事件将得到数传给子线程去进行计算
	caculateButton.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			int num = Integer.parseInt(input.getText().toString());
			Message msg = new Message();
			msg.what = 0x123;
			Bundle data = new Bundle();
			data.putInt("num", num);
			progressBar.setMax(num);
			progressBar.setMax(0);
			progressBar.setProgress(0);
			cacThread.getHandler().sendMessage(msg);
		}
	});
	// 当前线程来接收计算线程计算的结果
	mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x321) {
				List<Integer> nums = new ArrayList<Integer>();
				nums = (List<Integer>) msg.getData()
						.getSerializable("nums");
				output.setText(nums.toString());
			} else if (msg.what == 0x111) {
				int total = msg.getData().getInt("total");
				int current = msg.getData().getInt("current");
				progressBar.setMax(total);
				progressBar.setProgress(current);
			}
		}
	};
	cacThread = new CaculateThread(mHandler);
	cacThread.run();
}

}
这个是线程
public class CaculateThread extends Thread {

private CaculateHandler handler;
private Handler parentHandler;

public CaculateThread(Handler handler) {
	// 获取父线程
	parentHandler = handler;
	
}

@Override
public void run() {
	Looper.prepare();
	this.handler = new CaculateHandler(parentHandler);
	Looper.loop();
}

public CaculateHandler getHandler() {
	return handler;
}

public void setHandler(CaculateHandler handler) {
	this.handler = handler;
}

}
耗时操作的handler
public class CaculateHandler extends Handler {

private Handler parentHandler;
private int total = 0;
private int current = 0;

public CaculateHandler(Handler handler) {
	parentHandler = handler;
}

@Override
public void handleMessage(Message msg) {
	if (msg.what == 0x123) {
		int num = msg.getData().getInt("num");
		total = num;
		List<Integer> nums = new ArrayList<Integer>();
		for (int j = 3;; j = j + 2) {
			int k = 2;
			for (; k < j; k++) {
				if (j % k == 0) {
					break;
				}
			}
			// 为质数
			if (j == k) {
				nums.add(j);
				current = nums.size();
				{
					Message procMsg = new Message();
					procMsg.what = 0x111;
					Bundle data = new Bundle();
					data.putInt("total", total);
					data.putInt("current", current);
					procMsg.setData(data);
					// 将消息传给ui线程
					parentHandler.sendMessage(procMsg);
				}
				// 如果达到需要的数量,将质数发送出去
				if (nums.size() >= num) {
					Message outMsg = new Message();
					outMsg.what = 0x321;
					Bundle data = new Bundle();
					data.putSerializable("nums", (Serializable) nums);
					outMsg.setData(data);
					// 将消息传给ui线程
					parentHandler.sendMessage(outMsg);
				}
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

android framework

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值