当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
自动化运维工具Ansible(5)流程控制
(十二)树--哈夫曼树
ISCC2021——web部分
pgsql函数中的return类型
(TensorFlow)——tf.variable_scope和tf.name_scope详解
WARNING: sql version 9.2, server version 11.0.Some psql features might not work.
8.30难题留坑:计数器问题和素数等差数列问题
Android connects to mysql database using okhttp
Androd Day02
with recursive用法
flink-sql所有数据类型
flink on yarn指定第三方jar包
Redis持久化方式RDB和AOF详解
MySql的concat和group_concat的区别
AIDL communication between two APPs
安卓连接mysql数据库,使用okhttp
sql中group by的用法
编程Go:内置打印函数 print、println 和 fmt 包中 fmt.Print、fmt.Println 的区别
【树 图 科 技 头 条】2022年6月28日 星期二 伊能静做客树图社区
Upload靶场搭建&&第一二关