当前位置:网站首页>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);
});
}
}
边栏推荐
- 求组合数 AcWing 889. 满足条件的01序列
- P2575 master fight
- Leetcode-3: Longest substring without repeated characters
- C - XOR to all (binary topic)
- Single chip computer engineering experience - layered idea
- 1.14 - assembly line
- Traversal of leetcode tree
- There are three kinds of SQL connections: internal connection, external connection and cross connection
- Redis-01.初识Redis
- Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams
猜你喜欢
什么是套接字?Socket基本介绍
Game theory acwing 892 Steps Nim game
There are three kinds of SQL connections: internal connection, external connection and cross connection
Find the combination number acwing 888 Find the combination number IV
Leetcode array operation
5. Oracle TABLESPACE
【LeetCode】Easy | 20. Valid parentheses
求组合数 AcWing 889. 满足条件的01序列
vsCode创建自己的代码模板
1.13 - RISC/CISC
随机推荐
our solution
Traversal of leetcode tree
[moviepy] unable to find a solution for exe
Inclusion exclusion principle acwing 890 Divisible number
SQL三种连接:内连接、外连接、交叉连接
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
论文阅读报告
Vant Weapp SwipeCell设置多个按钮
Nested method, calculation attribute is not applicable, use methods
C - XOR to all (binary topic)
Simple selection sort of selection sort
Dataframe (1): introduction and creation of dataframe
ollvm编译出现的问题纪录
2048项目实现
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
International Open Source firmware Foundation (osff) organization
Ffmpeg build download (including old version)
Chapter 6 relational database theory
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations