当前位置:网站首页>Using handler in a new thread
Using handler in a new thread
2022-07-05 06:27:00 【v_ three billion four hundred and eighty-three million six hund】
- The main thread is automatically created Handler, So there's no need to initialize
. Be yourself new Thread When , Change, too ui Or prevent ANR Created on Handler
In the thread, you need 1,Looper.prepare() 2,newHandler rewrite handlerMessage 3,looper.loop
The following procedure is to transmit a number ,Toas Show the prime numbers inside .
package com.example.handl;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public final static String UPPER_NUM="upper";
private EditText etNum;
private CalThread calThread;
class CalThread extends Thread{
private Handler mHandler;
@Override
public void run() {
super.run();
Looper.prepare();
mHandler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0x123){
int upper = msg.getData().getInt(UPPER_NUM);
List<Integer> nums = new ArrayList<>();
outer:
for (int i = 2; i < upper; i++) {
int j=2;
while(j<Math.sqrt(i)){
if(i!=2&&i%j==0){
continue outer;
}
j++;
}
nums.add(i);
}
Toast.makeText(MainActivity.this,nums.toString(),Toast.LENGTH_LONG).show();
}
}
};
Looper.loop();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNum=findViewById(R.id.editText);
calThread= new CalThread();
calThread.start();
Button bn =findViewById(R.id.button);
bn.setOnClickListener(view -> {
Message msg=new Message();
msg.what=0x123;
Bundle bundle=new Bundle();
bundle.putInt(UPPER_NUM,Integer.parseInt(etNum.getText().toString()));
msg.setData(bundle);
calThread.mHandler.sendMessage(msg);
});
}
}
边栏推荐
- What is socket? Basic introduction to socket
- H5内嵌App适配暗黑模式
- 11-gorm-v2-02-create data
- C - XOR to all (binary topic)
- MySQL advanced part 2: MySQL architecture
- Chart. JS - Format Y axis - chart js - Formatting Y axis
- 博弈论 AcWing 892. 台阶-Nim游戏
- 1.14 - assembly line
- 【LeetCode】Easy | 20. Valid parentheses
- Niu Mei's math problems
猜你喜欢
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
Vant Weapp SwipeCell设置多个按钮
中国剩余定理 AcWing 204. 表达整数的奇怪方式
P2575 master fight
将webApp或者H5页面打包成App
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
[QT] QT multithreading development qthread
7.Oracle-表结构
随机推荐
How to understand the definition of sequence limit?
1.15 - input and output system
ollvm编译出现的问题纪录
Leetcode divide and conquer / dichotomy
Find the combination number acwing 889 01 sequence meeting conditions
[wustctf2020] plain_ WP
求组合数 AcWing 888. 求组合数 IV
LeetCode-54
博弈论 AcWing 894. 拆分-Nim游戏
背包问题 AcWing 9. 分组背包问题
How to generate an image from text on fly at runtime
FFmpeg build下载(包含old version)
Chart. JS - Format Y axis - chart js - Formatting Y axis
[QT] QT multithreading development qthread
our solution
1.14 - assembly line
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
论文阅读报告
Record the process of configuring nccl and horovod in these two days (original)
时间很快,请多做有意义的事情