当前位置:网站首页>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怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- 1.15 - input and output system
- How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
- [2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
- Operator priority, one catch, no doubt
- Applicable to Net free barcode API [off] - free barcode API for NET [closed]
- MySQL advanced part 2: optimizing SQL steps
- 博弈论 AcWing 892. 台阶-Nim游戏
- C - XOR to all (binary topic)
- Chart. JS - Format Y axis - chart js - Formatting Y axis
猜你喜欢

MySQL advanced part 2: optimizing SQL steps

5.Oracle-表空间

LeetCode 0107. Sequence traversal of binary tree II - another method

ollvm编译出现的问题纪录

Genesis builds a new generation of credit system
![[moviepy] unable to find a solution for exe](/img/0a/4841f53cedc1333654b9443e406f4c.jpg)
[moviepy] unable to find a solution for exe

MPLS experiment

博弈论 AcWing 893. 集合-Nim游戏

SQL三种连接:内连接、外连接、交叉连接

1.15 - input and output system
随机推荐
[learning] database: several cases of index failure
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
H5内嵌App适配暗黑模式
Redis-01.初识Redis
Winter messenger 2
LeetCode-54
MySQL advanced part 1: index
Leetcode heap correlation
5. Oracle TABLESPACE
Game theory acwing 894 Split Nim game
C job interview - casting and comparing - C job interview - casting and comparing
Niu Mei's math problems
Modnet matting model reproduction
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
11-gorm-v2-03-basic query
MySQL advanced part 1: triggers
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
P3265 [jloi2015] equipment purchase
高斯消元 AcWing 884. 高斯消元解异或线性方程组
MySQL advanced part 2: SQL optimization