当前位置:网站首页>Usage of Thread, Handler and IntentService
Usage of Thread, Handler and IntentService
2022-08-04 06:05:00 【N_Y_S】
Key points
Thread (thread), Handler (handle), IntentService
Content
When to use threads: When time-consuming tasks need to be performed in the main thread
When the handle is used: Pass the message from the child thread to the main thread.For example: Update UI
Android three ways to open child threads
1. Inherit Thread
Create a child thread method one inherits the Thread classclass MyThread extends Thread{@Overridepublic void run() {//Simulate time-consuming operationtry {sleep(3000); } catch (InterruptedException e) {e.printStackTrace();}tv_text.setText("123456789");An error will be reported here, because the interface of the ui thread cannot be changed after the thread performs time-consuming operationsOnly the original thread that created a view hierarchy can touch its views.}}MyThread myThread=new MyThread();myThread.start();//If you want to set the value of tv_text, you need to use the Handler handle
Second, inherit Runnable
Create a child thread method 2 inherit the Runnable classclass MyRunnable implements Runnable{@Overridepublic void run() {try {There is no sleep method in Runnable, so borrow the method in ThreadThread.sleep(3000);String result="123456";Log.i("Runnable:", result);} catch (InterruptedException e) {e.printStackTrace();}}}MyRunnable myRunnable=new MyRunnable();Thread thread=new Thread(myRunnable);thread.start();Three, use Thread and Runnable at the same time
//Create child thread method 3 Use both Thread and Runnable to create anonymous inner classesnew Thread(new Runnable() {@Overridepublic void run() {try {//Because it is currently in the Runnable interface, we still have to borrow the sleep method of ThreadThread.sleep(3000);String result="Anonymous thread";tv_text.setText(result);} catch (InterruptedException e) {e.printStackTrace();}}}).start();How to define Handler first:
//This method is outdated, generally adding the parameter Looper.getMainLooper()
//private Handler handler=new handler(){};
private Handler handler=new handler(Looper.getMainLooper){
};
//How to pass the message of the child thread to the child thread?
//In the thread's run()
Message msg=new Message();
msg.what=1;
msg.obj="content"
//0x111 is a hexadecimal number
handler.sendEmptyMessage(0x111);
Receive messages in the main thread
private Handler handler=new handler(Looper.getMainLooper){
handleMessage(Message msg){
if(msg.what==0x111){
String result=(Sring) msg.obj;
tv1.setText(result);
}
};
IntentService
IntentService is a subclass of Service
The difference between him and the Service is that the Service will not automatically open or close the thread, but the IntentService will open and close automatically.
边栏推荐
猜你喜欢

Logistic Regression --- Introduction, API Introduction, Case: Cancer Classification Prediction, Classification Evaluation, and ROC Curve and AUC Metrics

TensorFlow2学习笔记:7、优化器

TensorFlow2学习笔记:4、第一个神经网模型,鸢尾花分类

判断字符串是否有子字符串重复出现

VScode配置PHP环境

TensorFlow2 study notes: 4. The first neural network model, iris classification

NFT市场可二开开源系统

sklearn中的pipeline机制

【CV-Learning】卷积神经网络

flink-sql所有语法详解
随机推荐
简单说Q-Q图;stats.probplot(QQ图)
Matplotlib中的fill_between;np.argsort()函数
Thread 、Handler和IntentService的用法
flink on yarn任务迁移
8.30难题留坑:计数器问题和素数等差数列问题
sql中group by的用法
简单明了,数据库设计三大范式
数据库根据提纲复习
Dictionary feature extraction, text feature extraction.
多项式回归(PolynomialFeatures)
(十)树的基础部分(二)
剑指 Offer 2022/7/12
Learning curve learning_curve function in sklearn
Vulnhub:Sar-1
TensorFlow2学习笔记:7、优化器
【树 图 科 技 头 条】2022年6月28日 星期二 伊能静做客树图社区
iptables防火墙
彻底搞懂箱形图分析
二月、三月校招面试复盘总结(二)
双重指针的使用