当前位置:网站首页>在新线程中使用Handler
在新线程中使用Handler
2022-07-05 06:20:00 【v_3483608762】
- 主线程里自动创建了Handler,因此不需要初始化
。当自己new Thread时候,也要改变ui或者防止ANR时创建Handler
在线程中需要 1,Looper.prepare() 2,newHandler 重写handlerMessage 3,looper.loop
下面程序是传一个数,Toas显示里面的质数。
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);
});
}
}
边栏推荐
- Golang uses context gracefully
- Leetcode heap correlation
- MySQL advanced part 1: stored procedures and functions
- Nested method, calculation attribute is not applicable, use methods
- Introduction to LVS [unfinished (semi-finished products)]
- MPLS experiment
- Sqlmap tutorial (1)
- Sum of three terms (construction)
- Open source storage is so popular, why do we insist on self-development?
- SQLMAP使用教程(二)实战技巧一
猜你喜欢

Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations

2021apmcm post game Summary - edge detection

4. Object mapping Mapster

求组合数 AcWing 889. 满足条件的01序列

Appium automation test foundation - Summary of appium test environment construction

什么是套接字?Socket基本介绍

MySQL advanced part 1: stored procedures and functions
![[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis](/img/20/826cc9d514496955a557439881234d.jpg)
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis

Sqlmap tutorial (II) practical skills I

There are three kinds of SQL connections: internal connection, external connection and cross connection
随机推荐
[rust notes] 14 set (Part 1)
Usage scenarios of golang context
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
[rust notes] 13 iterator (Part 2)
Sum of three terms (construction)
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
MySQL advanced part 2: storage engine
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
[rust notes] 17 concurrent (Part 1)
传统数据库逐渐“难适应”,云原生数据库脱颖而出
Daily question 1189 Maximum number of "balloons"
[rust notes] 16 input and output (Part 1)
MySQL advanced part 1: triggers
Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams
C - XOR to all (binary topic)
A reason that is easy to be ignored when the printer is offline
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
Sqlmap tutorial (1)
博弈论 AcWing 891. Nim游戏
Overview of variable resistors - structure, operation and different applications