当前位置:网站首页>Handler操作记录 Only one Looper may be created per thread
Handler操作记录 Only one Looper may be created per thread
2022-07-27 05:12:00 【android framework】
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);
}
}
}
}
}
边栏推荐
- 记一次PG主从搭建及数据同步性能测试流程
- You can't even do a simple function test well. What do you take to talk about salary increase with me?
- In the future, face brushing payment can occupy a lot of market share
- Choose futures companies with state-owned enterprise background to open accounts
- GBase 8c产品简介
- 万字解析MySQL索引原理——InnoDB索引结构与读取
- MySQL如何执行查询语句
- 怎样才能拿到期货开户最低的手续费?
- 什么是okr,和kpi的区别在哪里
- 神芷迦蓝寺
猜你喜欢

Minio8.x version setting policy bucket policy

市场冷空气来袭,SeekTiger如何逆流而上?

Sealem Finance - a new decentralized financial platform based on Web3

Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers

你连简单的功能测试都做不好,你拿什么来跟我谈涨薪?

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

SeekTiger即将上线STI聚变Mining功能,获取OKA通证

Choose a qualified futures company to open an account

SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?

期货公司开户的具体事项
随机推荐
2020年PHP中级面试知识点及答案
解决MySQL JDBC数据批量插入慢的问题
手把手教你搭建钉钉预警机器人
Face brushing payment will never be out of date, but will continue to change
How to open a general commodity futures account
怎么开立普通商品期货账户
NFT new paradigm, okaleido innovation NFT aggregation trading ecosystem
Sealem Finance - a new decentralized financial platform based on Web3
一文读懂Elephant Swap的LaaS方案的优势之处
期货开户怎么选择好的期货公司 ?
PHP的CI框架学习
Dimitra and ocean protocol interpret the secrets behind agricultural data
The business logic of face brushing changed significantly, and merchants vied for war smoke to rise again
MySQL如何执行查询语句
GBase 8c产品简介
The difference between for... Of and for... In JS
How to judge whether a property belongs to an instance object or inherits from a constructor in JS
Which futures company has a low handling fee and a high refund?
Mysql分组后时并行统计数量
Characteristics of hexadecimal