当前位置:网站首页>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);
});
}
}
边栏推荐
- Paper reading report
- Find the combination number acwing 889 01 sequence meeting conditions
- confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
- C job interview - casting and comparing - C job interview - casting and comparing
- 5. Oracle TABLESPACE
- Game theory acwing 892 Steps Nim game
- Leetcode heap correlation
- 【LeetCode】Day94-重塑矩阵
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- 论文阅读报告
猜你喜欢

将webApp或者H5页面打包成App

求组合数 AcWing 887. 求组合数 III
![[moviepy] unable to find a solution for exe](/img/0a/4841f53cedc1333654b9443e406f4c.jpg)
[moviepy] unable to find a solution for exe

4.Oracle-重做日志文件管理

International Open Source firmware Foundation (osff) organization
![[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields](/img/65/7fa32cd0005ddaaebacd85c25e0c7e.jpg)
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields

博弈论 AcWing 891. Nim游戏

MySQL advanced part 1: stored procedures and functions

‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

LeetCode-61
随机推荐
LeetCode 1200. Minimum absolute difference
求组合数 AcWing 888. 求组合数 IV
There are three kinds of SQL connections: internal connection, external connection and cross connection
容斥原理 AcWing 890. 能被整除的数
Relevant information of National Natural Science Foundation of China
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
求组合数 AcWing 889. 满足条件的01序列
Basic explanation of typescript
Day 2 document
MySQL advanced part 1: triggers
P3265 [jloi2015] equipment purchase
Leetcode heap correlation
Leetcode backtracking method
LeetCode 0107. Sequence traversal of binary tree II - another method
3.Oracle-控制文件的管理
AE tutorial - path growth animation
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
VLAN experiment
One question per day 1020 Number of enclaves
Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams