当前位置:网站首页>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);
});
}
}
边栏推荐
- Idea debug failed
- Find the combination number acwing 889 01 sequence meeting conditions
- [2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
- 1.手动创建Oracle数据库
- Operator priority, one catch, no doubt
- 阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
- 3.Oracle-控制文件的管理
- Winter vacation water test 1 Summary
- Find the combination number acwing 888 Find the combination number IV
- 2022-5-the fourth week daily
猜你喜欢

How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
![[moviepy] unable to find a solution for exe](/img/0a/4841f53cedc1333654b9443e406f4c.jpg)
[moviepy] unable to find a solution for exe

5. Oracle tablespace

【LeetCode】Easy | 20. Valid parentheses

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

Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022

1.14 - assembly line

背包问题 AcWing 9. 分组背包问题
![[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields](/img/65/7fa32cd0005ddaaebacd85c25e0c7e.jpg)
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields

Knapsack problem acwing 9 Group knapsack problem
随机推荐
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
P2575 master fight
ollvm编译出现的问题纪录
Operator priority, one catch, no doubt
Relevant information of National Natural Science Foundation of China
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
MQClientException: No route info of this topic: type_ topic
求组合数 AcWing 889. 满足条件的01序列
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
Inclusion exclusion principle acwing 890 Divisible number
博弈论 AcWing 894. 拆分-Nim游戏
Client use of Argo CD installation
容斥原理 AcWing 890. 能被整除的数
Design specification for mobile folding screen
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
4.Oracle-重做日志文件管理
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
VLAN experiment