当前位置:网站首页>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);
});
}
}
边栏推荐
- NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
- 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
- MySQL advanced part 1: stored procedures and functions
- 2021apmcm post game Summary - edge detection
- 20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
- Leetcode dynamic programming
- 阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
- Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams
- Leetcode-9: palindromes
- 11-gorm-v2-02-create data
猜你喜欢
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
博弈论 AcWing 893. 集合-Nim游戏
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
博弈论 AcWing 891. Nim游戏
4. 对象映射 - Mapping.Mapster
Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
容斥原理 AcWing 890. 能被整除的数
Vant weave swipecell sets multiple buttons
区间问题 AcWing 906. 区间分组
MySQL advanced part 2: optimizing SQL steps
随机推荐
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
[QT] QT multithreading development qthread
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
Vant weave swipecell sets multiple buttons
Client use of Argo CD installation
4.Oracle-重做日志文件管理
2021apmcm post game Summary - edge detection
5.Oracle-表空间
Leetcode backtracking method
Chart. JS - Format Y axis - chart js - Formatting Y axis
Paper reading report
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
Redis-01.初识Redis
Operator priority, one catch, no doubt
5.Oracle-錶空間
2022-5-第四周日报
11-gorm-v2-03-basic query
P3265 [jloi2015] equipment purchase
背包问题 AcWing 9. 分组背包问题