当前位置:网站首页>在新线程中使用Handler
在新线程中使用Handler
2022-07-05 06:20:00 【v_3483608762】
- 主线程里自动创建了Handler,因此不需要初始化
。当自己new Thread时候,也要改变ui或者防止ANR时创建Handler
在线程中需要 1,Looper.prepare() 2,newHandler 重写handlerMessage 3,looper.loop
下面程序是传一个数,Toas显示里面的质数。
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);
});
}
}
边栏推荐
- Chart. JS - Format Y axis - chart js - Formatting Y axis
- 2048项目实现
- TypeScript 基础讲解
- The difference between CPU core and logical processor
- Operator priority, one catch, no doubt
- MySQL advanced part 2: optimizing SQL steps
- 【LeetCode】Day95-有效的数独&矩阵置零
- Sum of three terms (construction)
- Sqlmap tutorial (II) practical skills I
- Winter messenger 2
猜你喜欢
WordPress switches the page, and the domain name changes back to the IP address
QQ computer version cancels escape character input expression
LeetCode-54
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
高斯消元 AcWing 884. 高斯消元解异或线性方程组
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
论文阅读报告
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Leetcode stack related
1.14 - assembly line
随机推荐
MySQL advanced part 2: storage engine
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Open source storage is so popular, why do we insist on self-development?
Groupbykey() and reducebykey() and combinebykey() in spark
P3265 [jloi2015] equipment purchase
高斯消元 AcWing 884. 高斯消元解异或線性方程組
Chart. JS - Format Y axis - chart js - Formatting Y axis
Leetcode-6111: spiral matrix IV
SQL三种连接:内连接、外连接、交叉连接
One question per day 1020 Number of enclaves
Leetcode-9: palindromes
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
栈 AcWing 3302. 表达式求值
Leetcode-6108: decrypt messages
Records of some tools 2022
The difference between CPU core and logical processor
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
Leetcode stack related
Daily question 1189 Maximum number of "balloons"
Currently clicked button and current mouse coordinates in QT judgment interface