当前位置:网站首页>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);
});
}
}
边栏推荐
- LeetCode 1200. Minimum absolute difference
- International Open Source firmware Foundation (osff) organization
- Leetcode recursion
- Currently clicked button and current mouse coordinates in QT judgment interface
- 阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
- Leetcode stack related
- Chinese remainder theorem acwing 204 Strange way of expressing integers
- 微信小程序路由再次跳转不触发onload
- NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
- WordPress switches the page, and the domain name changes back to the IP address
猜你喜欢

What is socket? Basic introduction to socket

ollvm编译出现的问题纪录

MySQL advanced part 2: the use of indexes

Chapter 6 relational database theory
![[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields](/img/65/7fa32cd0005ddaaebacd85c25e0c7e.jpg)
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields

Find the combination number acwing 889 01 sequence meeting conditions

1.13 - RISC/CISC

20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore

安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel

3. Oracle control file management
随机推荐
区间问题 AcWing 906. 区间分组
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
How to generate an image from text on fly at runtime
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
AE tutorial - path growth animation
Simple selection sort of selection sort
[learning] database: several cases of index failure
Operator priority, one catch, no doubt
Inclusion exclusion principle acwing 890 Divisible number
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
MySQL advanced part 1: stored procedures and functions
H5 模块悬浮拖动效果
2048 project realization
Record the process of configuring nccl and horovod in these two days (original)
Leetcode-9: palindromes
MySQL advanced part 2: SQL optimization
Leetcode array operation
博弈论 AcWing 891. Nim游戏
MPLS experiment
2.Oracle-数据文件的添加及管理