当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Thread 、Handler和IntentService的用法
sql中group by的用法
属性动画的用法 以及ButterKnife的用法
flink on yarn任务迁移
网络大作业心得笔记
NFT市场可二开开源系统
编程Go:内置打印函数 print、println 和 fmt 包中 fmt.Print、fmt.Println 的区别
Logistic Regression --- Introduction, API Introduction, Case: Cancer Classification Prediction, Classification Evaluation, and ROC Curve and AUC Metrics
安装dlib踩坑记录,报错:WARNING: pip is configured with locations that require TLS/SSL
自动化运维工具Ansible(5)流程控制
(十二)树--哈夫曼树
Androd Day02
SQL练习 2022/7/5
flink-sql大量使用案例
flink onTimer定时器实现定时需求
自动化运维工具Ansible(7)roles
ISCC2021——web部分
Matplotlib中的fill_between;np.argsort()函数
Commons Collections2
(十一)树--堆排序