当前位置:网站首页>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);
});
}
}
边栏推荐
- Chinese remainder theorem acwing 204 Strange way of expressing integers
- C - XOR to all (binary topic)
- Presentation of attribute value of an item
- Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
- How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
- How to generate an image from text on fly at runtime
- 博弈论 AcWing 893. 集合-Nim游戏
- LeetCode 1200. Minimum absolute difference
- There are three kinds of SQL connections: internal connection, external connection and cross connection
- MPLS experiment
猜你喜欢

Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations

LeetCode-54

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

LeetCode-61

20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
![[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields](/img/65/7fa32cd0005ddaaebacd85c25e0c7e.jpg)
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields

1.13 - RISC/CISC

Find the combination number acwing 887 Find combination number III

Game theory acwing 893 Set Nim game

1.15 - input and output system
随机推荐
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
[leetcode] day95 effective Sudoku & matrix zeroing
Vant Weapp SwipeCell设置多个按钮
4. 对象映射 - Mapping.Mapster
Presentation of attribute value of an item
MySQL advanced part 1: index
Leetcode recursion
【LeetCode】Day95-有效的数独&矩阵置零
MPLS experiment
Game theory acwing 893 Set Nim game
Design specification for mobile folding screen
The route of wechat applet jumps again without triggering onload
容斥原理 AcWing 890. 能被整除的数
Niu Mei's math problems
AE tutorial - path growth animation
Usage scenarios of golang context
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
如何正确在CSDN问答进行提问
H5 模块悬浮拖动效果
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