当前位置:网站首页>在新线程中使用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);
});
}
}
边栏推荐
- 1040 Longest Symmetric String
- Multi screen computer screenshots will cut off multiple screens, not only the current screen
- Appium foundation - use the first demo of appium
- 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】Day94-重塑矩阵
- Shutter web hardware keyboard monitoring
- our solution
- [learning] database: several cases of index failure
- Overview of variable resistors - structure, operation and different applications
- JS quickly converts JSON data into URL parameters
猜你喜欢
![Introduction to LVS [unfinished (semi-finished products)]](/img/72/d5a943a8d6d71823dcbd7f23dda35b.png)
Introduction to LVS [unfinished (semi-finished products)]

中国剩余定理 AcWing 204. 表达整数的奇怪方式
![[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian](/img/f1/e7a8a1a31bc5712d9f32d91305a2b0.jpg)
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian

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

QQ computer version cancels escape character input expression

求组合数 AcWing 888. 求组合数 IV

Leetcode stack related

3.Oracle-控制文件的管理

Matrixdb V4.5.0 was launched with a new mars2 storage engine!

Appium automation test foundation - Summary of appium test environment construction
随机推荐
Leetcode-3: Longest substring without repeated characters
Leetcode divide and conquer / dichotomy
Leetcode-9: palindromes
Redis publish subscribe command line implementation
Data visualization chart summary (I)
SQL三种连接:内连接、外连接、交叉连接
开源存储这么香,为何我们还要坚持自研?
LeetCode-61
Leetcode stack related
P2575 master fight
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Leetcode-6110: number of incremental paths in the grid graph
1.14 - assembly line
MPLS experiment
什么是套接字?Socket基本介绍
AE tutorial - path growth animation
4. 对象映射 - Mapping.Mapster
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
P3265 [jloi2015] equipment purchase
SQLMAP使用教程(一)