当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- GBASE 8C——SQL参考6 sql语法(12)
- 【好文种草】根域名的知识 - 阮一峰的网络日志
- jenkins构建镜像自动化部署
- go通过channel获取goroutine的处理结果
- How to realize master-slave synchronization in mysql5.7
- golang控制goroutine数量以及获取处理结果
- ES对比两个索引的数据差
- Jenkins build image automatic deployment
- Aquanee will land in gate and bitmart in the near future, which is a good opportunity for low-level layout
- 记一次PG主从搭建及数据同步性能测试流程
猜你喜欢

PHP 实现与MySQL的数据交互

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

你真的了解 Session 和 Cookie 吗?

inno setup 打包 jar + h5 + mysql + redis 成 exe

Minio8.x version setting policy bucket policy

The NFT market pattern has not changed. Can okaleido set off a new round of waves?

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

Specific matters of opening accounts of futures companies

18.卷积神经网络

How to apply for the return of futures account opening company?
随机推荐
Mysql分组后时并行统计数量
vim编辑器全部删除文件内容
17.动量与学习率的衰减
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
2.简单回归问题
3.分类问题---手写数字识别初体验
Count the quantity in parallel after MySQL grouping
php 定义数组使用逗号,
Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding
The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
dbswitch数据迁移数据增量时如何不覆盖目标源数据
Activity之应用进程创建流程简析
13.逻辑回归
1.PyTorch简介
How MySQL and redis ensure data consistency
mysql优化sql相关(持续补充)
GBASE 8C——SQL参考4 字符集支持
MySQL索引分析除了EXPLAIN还有什么方法
The main advantage of face brushing payment users is their high degree of intelligence