安卓-通过Handler让主线程给子线程发送消息的实现
文章目录
前言
安卓-通过Handler,主线程与子线程互发消息。
一、演示通过Handler让子线程给主线程发送消息的实现;以及演示通过Handler让主线程给子线程发送消息的实现
1.java代码
演示通过Handler让子线程给主线程发送消息的实现;以及演示通过Handler让主线程给子线程发送消息的实现
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
/**
* 演示通过Handler让子线程给主线程发送消息的实现。
* 演示通过Handler让主线程给子线程发送消息的实现。
*/
public class MainActivity2 extends Activity {
private TextView tv_title;
private Button button,button2;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Toast.makeText(MainActivity2.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
}
};
private Handler subHandler;
private Looper myLooper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_title = findViewById(R.id.tv_title);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
new Thread(new Runnable() {
@Override
public void run() {
/**
* (1)创建了Looper对象,然后Looper对象中创建了MessageQueue
* (2)并将当前的Looper对象跟当前的线程(子线程)绑定ThreadLocal
*/
Looper.prepare();
/**
* (3)创建了handler对象,然后从当前线程中获取Looper对象,然后获取到MessageQueue对象
*/
subHandler = new Handler(){//如果前面没有写Looper.prepare(),则执行此行代码会报错:java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()
@Override
public void handleMessage(Message msg) {
Toast.makeText(MainActivity2.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();//执行此行时不会报错,因为前面执行了Looper.prepare();
//tv_title.setText(msg.obj.toString());//执行此行时报错:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.//因为view操作只能在主线程执行。
}
};
myLooper = Looper.myLooper();
/**
* (4)从当前线程中找到之前创建的Looper对象,然后找到MessageQueue
* (5)开启死循环,遍历消息池中的消息
* (6)当获取到msg的时候,调用这个msg的handler的disPatchMsg方法,让msg执行起来
*/
Looper.loop();
Log.d("tag","loop()方法执行完了");
}
}).start();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMsg();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMsg2();
}
});
}
/**
* 演示让子线程给主线程发送消息的实现
*/
private void sendMsg() {
new Thread(new Runnable() {
@Override
public void run() {
//Toast.makeText(MainActivity2.this,"测试",Toast.LENGTH_SHORT).show();//执行此行时报错:java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
SystemClock.sleep(2000);//模拟一个耗时操作
Message msg = new Message();
msg.what = 1;//区分发送的消息
msg.obj = "来自子线程的问候";
handler.sendMessage(msg);
}
}).start();
}
/**
* 演示让主线程给子线程发送消息的实现
*/
private void sendMsg2() {
subHandler.obtainMessage(2,"我是主线程发送来的祝福").sendToTarget();
}
@Override
protected void onDestroy() {
super.onDestroy();
//当Activity退出的时候终止Looper循环
if(myLooper != null){
myLooper.quit();//执行了此行代码,上面的Looper.loop();才会执行完。
myLooper = null;
}
}
}
2.布局文件
布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="让子线程给主线程发送消息"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="让主线程给子线程发送消息"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
总结
以上就是今天要讲的内容,本文仅仅简单介绍了通过Handler让子线程给主线程发送消息的实现;以及通过Handler让主线程给子线程发送消息的实现。