当前位置:网站首页>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
- Dataframe (1): introduction and creation of dataframe
- NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
- 4. Object mapping Mapster
- How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
- 容斥原理 AcWing 890. 能被整除的数
- La redirection de l'applet Wechat ne déclenche pas onload
- Daily question 1189 Maximum number of "balloons"
- There are three kinds of SQL connections: internal connection, external connection and cross connection
猜你喜欢

MySQL advanced part 1: stored procedures and functions

vsCode创建自己的代码模板

背包问题 AcWing 9. 分组背包问题

Simple selection sort of selection sort

Genesis builds a new generation of credit system

5.Oracle-表空间

WordPress switches the page, and the domain name changes back to the IP address

Vant Weapp SwipeCell设置多个按钮

Single chip computer engineering experience - layered idea

3. Oracle control file management
随机推荐
Leetcode dynamic programming
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
求组合数 AcWing 887. 求组合数 III
11-gorm-v2-02-create data
Leetcode array operation
4.Oracle-重做日志文件管理
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Leetcode-3: Longest substring without repeated characters
MySQL advanced part 1: triggers
H5 模块悬浮拖动效果
【LeetCode】Day95-有效的数独&矩阵置零
MySQL advanced part 2: SQL optimization
The route of wechat applet jumps again without triggering onload
4. Object mapping Mapster
Daily question 1189 Maximum number of "balloons"
New title of module a of "PanYun Cup" secondary vocational network security skills competition
Presentation of attribute value of an item
LeetCode 0107. Sequence traversal of binary tree II - another method
2048项目实现