当前位置:网站首页>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语法(15)
- rk3399 gpio口 如何查找是哪个gpio口
- dbswitch数据迁移数据增量时如何不覆盖目标源数据
- GBASE 8C——SQL参考6 sql语法(14)
- Day 17.The role of news sentiment in oil futures returns and volatility forecasting
- How can I get the lowest handling charge for opening a futures account?
- Sealem Finance - a new decentralized financial platform based on Web3
- 【并发编程系列9】阻塞队列之PriorityBlockingQueue,DelayQueue原理分析
- dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
- Inno setup package jar + H5 + MySQL + redis into exe
猜你喜欢

7.合并与分割

Getaverse, a distant bridge to Web3

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

MySQL索引优化相关原理

我想不通,MySQL 为什么使用 B+ 树来作索引?

Aquanee will land in gate and bitmart in the near future, which is a good opportunity for low-level layout

Day 9. Graduate survey: A love–hurt relationship

Graph node deployment

8.数学运算与属性统计

Numpy基础学习
随机推荐
Mysql5.7版本如何实现主从同步
Docker deploys the stand-alone version of redis - modify the redis password and persistence method
Which futures company do you go to and how do you open an account?
MySQL查询操作索引优化实践
How to realize master-slave synchronization in mysql5.7
18.卷积神经网络
go通过channel获取goroutine的处理结果
golang控制goroutine数量以及获取处理结果
andorid检测GPU呈现速度和过度绘制
3.分类问题---手写数字识别初体验
Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
golang中slice切片使用的误区
Handler操作记录 Only one Looper may be created per thread
Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter
GBASE 8C——SQL参考 5 全文检索
When opening futures accounts, you should discuss the policy in detail with the customer manager
如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分
Move protocol launched a beta version, and you can "0" participate in p2e
4.张量数据类型和创建Tensor
How to apply for the return of futures account opening company?