当前位置:网站首页>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);
});
}
}
边栏推荐
- MySQL advanced part 1: index
- 4. 对象映射 - Mapping.Mapster
- Basic explanation of typescript
- [wustctf2020] plain_ WP
- Leetcode-1200: minimum absolute difference
- Quickly use Amazon memorydb and build your own redis memory database
- 安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
- Redis-02.Redis命令
- Modnet matting model reproduction
- The route of wechat applet jumps again without triggering onload
猜你喜欢

求组合数 AcWing 889. 满足条件的01序列

博弈论 AcWing 894. 拆分-Nim游戏

求组合数 AcWing 888. 求组合数 IV

There are three kinds of SQL connections: internal connection, external connection and cross connection

求组合数 AcWing 887. 求组合数 III

WordPress switches the page, and the domain name changes back to the IP address

International Open Source firmware Foundation (osff) organization

5.Oracle-錶空間

区间问题 AcWing 906. 区间分组

Bit of MySQL_ OR、BIT_ Count function
随机推荐
如何正确在CSDN问答进行提问
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
求组合数 AcWing 887. 求组合数 III
Quickly use Amazon memorydb and build your own redis memory database
Chapter 6 relational database theory
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Leetcode-9: palindromes
[leetcode] day94 reshape matrix
MPLS experiment
博弈论 AcWing 892. 台阶-Nim游戏
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
Chinese remainder theorem acwing 204 Strange way of expressing integers
Interval problem acwing 906 Interval grouping
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
Stack acwing 3302 Expression evaluation
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
Currently clicked button and current mouse coordinates in QT judgment interface
Game theory acwing 892 Steps Nim game
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Knapsack problem acwing 9 Group knapsack problem