android初学者请教关于Android线程间通信的消息机制的一个小问题

and_xl5148 2015-01-05 12:35:37
我实现了一个主线程发送消息,子线程处理消息的小程序,用来计算输入值以内的质数。


如图所示,是用Toast弹出的结果。现在我想用TextView显示结果。而子线程不能直接操作UI元素,所以要发送消息返回主线程操作。代码中注释的是我做的,但是是错误的。请大神指出错误的地方和应该怎么做(尤其是用message怎么传递set对象)。

代码如下:


import java.util.HashSet;
import java.util.Set;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main1Activity extends Activity {
private EditText et;
private TextView tv;
private CalThread thread;
private final int FLAG = 0x123;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
et = (EditText)findViewById(R.id.editText1);
tv = (TextView)findViewById(R.id.tv);
thread = new CalThread();//创建子线程并启动子线程
thread.start();
}

/** private Handler handler1 = new Handler() { //接受和处理消息
public void handleMessage(Message msg1) {
tv.setText(et.getText().toString()+"以内的质数有:"+msg1.toString());
}
};*/


public class CalThread extends Thread{ //子线程
public Handler handler;
public void run(){
Looper.prepare();//创建当前线程的Looper对象---->创建消息队列
handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what ==FLAG){
Set<Integer> set = new HashSet<Integer>();
int num = msg.arg1;
test:for(int i=2;i<=num;i++){//判断2-输入值之间的质数
for(int j=2;j<=Math.sqrt(i);j++){
if(i!=2&&i%j==0){//不是质数,跳出循环
continue test;
}
}
set.add(i);
}
Toast.makeText(Main1Activity.this, set.toString(), Toast.LENGTH_SHORT).show();

/**Message msg1 = new Message();
msg1.obj = set;
handler1.sendMessage(msg);*/
}
}
};
Looper.loop();//死循环读取消息队列中的消息
}
}

public void myCal(View view){ //按钮事件
String msg = et.getText().toString();
Message m = new Message();
m.what = FLAG;
m.arg1 = Integer.parseInt(msg);
thread.handler.sendMessage(m);
}
}

...全文
115 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
and_xl5148 2015-01-05
  • 打赏
  • 举报
回复
学习了,能解释下主线程接受处理消息的时候,为什么嵌套两个判断
_xianfeng99 2015-01-05
  • 打赏
  • 举报
回复

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);
		et = (EditText) findViewById(R.id.editText1);
		tv = (TextView) findViewById(R.id.tv);
		// thread = new CalThread();// 创建子线程并启动子线程
		// thread.start();
	}

	/**
	 * private Handler handler1 = new Handler() { //接受和处理消息 public void
	 * handleMessage(Message msg1) {
	 * tv.setText(et.getText().toString()+"以内的质数有:"+msg1.toString()); } };
	 */

	public Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == FLAG) {
				if (msg.what == FLAG) {
					Set<Integer> set = msg.obj;
					// 把值赋值给TextView就可以显示了,这个你自己写吧
					// tv.setText(...);
				}
			}
		}
	};

	public class CalThread extends Thread { // 子线程

		private int num;

		public CalThread(int num) {
			this.num = num;
		}

		public void run() {
			Set<Integer> set = new HashSet<Integer>();
			// int num = msg.arg1;
			test: for (int i = 2; i <= num; i++) {// 判断2-输入值之间的质数
				for (int j = 2; j <= Math.sqrt(i); j++) {
					if (i != 2 && i % j == 0) {// 不是质数,跳出循环
						continue test;
					}
				}
				set.add(i);
				// 如果先在这个时候通知主线程更新UI可以在这里调用Handler(注意这个Handler是在主线程中声明定义的)

				Message msg = handler.obtainMessage();
				msg.what = FLAG;
				msg.obj = set;
				handler.sendMessage(msg);
			}
		}
	}

	public void myCal(View view) { // 按钮事件
		String msg = et.getText().toString();
		// Message m = new Message();
		// m.what = FLAG;
		// m.arg1 = Integer.parseInt(msg);
		// thread.handler.sendMessage(m);

		thread = new CalThread(Integer.parseInt(msg));// 创建子线程并启动子线程
		thread.start();
	}

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧