当前位置:网站首页>在新线程中使用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);
});
}
}
边栏推荐
- Day 2 document
- Quickly use Amazon memorydb and build your own redis memory database
- Sum of three terms (construction)
- Presentation of attribute value of an item
- 1.14 - assembly line
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- SQLMAP使用教程(二)实战技巧一
- P2575 master fight
- Leetcode-6110: number of incremental paths in the grid graph
- 博弈论 AcWing 892. 台阶-Nim游戏
猜你喜欢

Error ora-28547 or ora-03135 when Navicat connects to Oracle Database

1.15 - input and output system

MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!

5.Oracle-表空间

MySQL advanced part 1: View

求组合数 AcWing 889. 满足条件的01序列
![[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis](/img/20/826cc9d514496955a557439881234d.jpg)
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis

Overview of variable resistors - structure, operation and different applications

容斥原理 AcWing 890. 能被整除的数

Client use of Argo CD installation
随机推荐
[rust notes] 14 set (Part 2)
Leetcode divide and conquer / dichotomy
博弈论 AcWing 894. 拆分-Nim游戏
Leetcode-556: the next larger element III
LeetCode 1200. Minimum absolute difference
C job interview - casting and comparing - C job interview - casting and comparing
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
开源存储这么香,为何我们还要坚持自研?
Doing SQL performance optimization is really eye-catching
There are three kinds of SQL connections: internal connection, external connection and cross connection
[rust notes] 17 concurrent (Part 2)
Operator priority, one catch, no doubt
MySQL advanced part 2: SQL optimization
WordPress switches the page, and the domain name changes back to the IP address
RGB LED infinite mirror controlled by Arduino
JS quickly converts JSON data into URL parameters
Currently clicked button and current mouse coordinates in QT judgment interface
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
[rust notes] 15 string and text (Part 1)
Leetcode-6110: number of incremental paths in the grid graph