当前位置:网站首页>Only one looper may be created per thread
Only one looper may be created per thread
2022-07-27 05:54: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);
// Clicking on the event will send the data to the sub thread for calculation
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);
}
});
// The current thread receives the calculation result of the calculation thread
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();
}
}
This is a thread
public class CaculateThread extends Thread {
private CaculateHandler handler;
private Handler parentHandler;
public CaculateThread(Handler handler) {
// Get parent thread
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;
}
}
Time consuming operation 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;
}
}
// Prime number
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);
// Send the message to ui Threads
parentHandler.sendMessage(procMsg);
}
// If the required quantity is reached , Send prime numbers
if (nums.size() >= num) {
Message outMsg = new Message();
outMsg.what = 0x321;
Bundle data = new Bundle();
data.putSerializable("nums", (Serializable) nums);
outMsg.setData(data);
// Send the message to ui Threads
parentHandler.sendMessage(outMsg);
}
}
}
}
}
边栏推荐
- dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
- MySQL索引分析除了EXPLAIN还有什么方法
- In the future, face brushing payment can occupy a lot of market share
- MySQL索引优化相关原理
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- Face brushing payment will never be out of date, but will continue to change
- Personal collection code cannot be used for business collection
- rk3399 gpio口 如何查找是哪个gpio口
- 13.逻辑回归
- MySQL快速比较数据库表数据
猜你喜欢

How to open a general commodity futures account

Count the quantity in parallel after MySQL grouping

新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic

万字解析MySQL索引原理——InnoDB索引结构与读取

Mysql分组后时并行统计数量

PHP 实现与MySQL的数据交互

jenkins构建镜像自动化部署

Move protocol launched a beta version, and you can "0" participate in p2e

如果在线上遇到了OOM,该如何解决?

The written test questions of 25 large Internet companies are summarized, and I have encountered packages.
随机推荐
Rating and inquiry details of futures companies
GBASE 8C——SQL参考6 sql语法(4)
使用Docker部署Redis进行高可用主从复制
我想不通,MySQL 为什么使用 B+ 树来作索引?
Which futures company do you go to and how do you open an account?
minio8.x版本设置policy桶策略
身为技术管理者应该具备的素质(猜想)
Specific matters of opening accounts of futures companies
GBase 8c产品简介
GBASE 8C——SQL参考6 sql语法(10)
Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例
Jenkins build image automatic deployment
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
14.实例-多分类问题
MySQL查询操作索引优化实践
15.GPU加速、minist测试实战和visdom可视化
golang怎么给空结构体赋值
GBASE 8C——SQL参考6 sql语法(13)
vscode打造golang开发环境以及golang的debug单元测试
PHP的CI框架学习