当前位置:网站首页>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);
});
}
}
边栏推荐
- 5. Oracle TABLESPACE
- 1.15 - input and output system
- Presentation of attribute value of an item
- Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
- 20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
- Inclusion exclusion principle acwing 890 Divisible number
- LeetCode 0107. Sequence traversal of binary tree II - another method
- Client use of Argo CD installation
- Idea debug failed
- How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
猜你喜欢
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
Chinese remainder theorem acwing 204 Strange way of expressing integers
博弈论 AcWing 893. 集合-Nim游戏
Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
MySQL advanced part 2: storage engine
What is socket? Basic introduction to socket
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
Knapsack problem acwing 9 Group knapsack problem
[wustctf2020] plain_ WP
高斯消元 AcWing 884. 高斯消元解异或线性方程组
随机推荐
[moviepy] unable to find a solution for exe
【LeetCode】Day95-有效的数独&矩阵置零
There are three kinds of SQL connections: internal connection, external connection and cross connection
博弈论 AcWing 891. Nim游戏
Nested method, calculation attribute is not applicable, use methods
Bit of MySQL_ OR、BIT_ Count function
MySQL advanced part 1: View
Basic explanation of typescript
TCP's understanding of three handshakes and four waves
将webApp或者H5页面打包成App
[wustctf2020] plain_ WP
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
博弈论 AcWing 894. 拆分-Nim游戏
2022/6/29-日报
11-gorm-v2-03-basic query
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Winter vacation water test 1 Summary
Knapsack problem acwing 9 Group knapsack problem
Leetcode heap correlation
中国剩余定理 AcWing 204. 表达整数的奇怪方式