文章目录
Handler介绍
Handler 是 Android SDK 来处理异步消息的核心类
子线程与主线程通过 Handler 来进行通信。子线程可以通过 Handler 来通知主线程进行UI更新。
【使用Handler对象发送消息】
sendMessage???()
系列方法
sendEmptyMessage???()
系列方法
post???()
系列方法
以上发送消息的各种方式都是基于对应的 sendMessage()
方法实现的
Handler 顾名思义也就是处理者的意思,它主要是用于发送和处理消息的。发送消息一般是使用 Handler 的 sendMessage()
方法,而发出的消息经过一系列地辗转处理后, 最终会传递到 Handler 的 handleMessage()
方法中
实现3s后跳转主界面
主要代码:
SplashActivity
public class SplashActivity extends AppCompatActivity {
private Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler(){
public void handleMessage(Message msg) {
Intent intent = new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
};
//延迟发送消息
handler.sendEmptyMessageDelayed(0,3000);
}
}
处理消息
【处理消息】
方式1:自定义类继承自 android.os.Handler
,并重写 void handleMessage(Message)
方式2:自定义类实现 android.os.Handler.Callback
,实现接口中的 bean handleMessage(Message)
方法,并创建自定义类的对象,将该对象用于创建 Handle 对象的构造方法中
方式3:通过 Message.obtain(Handler, Runnable)
方法获取 Message 对象,其中 Runnable 对象的 public void run()
方法就是处理消息的方法
【处理消息的分发】
如果以上处理消息的多种方式共存,则
1、如果存在方式 3,直接通过方式 3 处理,而方式 1 和方式 2 无效
2、如果只存在方式 1 和方式 2,则方式 2 优于方式 1;方式 2 的 boolean handleMessage()
如果返回 true,则方式 1 无效
现在用方式 2 来做上一节 进度条栗子
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
private Button button;
private TextView textView;
private Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
button = findViewById(R.id.button);
textView = findViewById(R.id.tv_show_progress);
button.setOnClickListener(this);
InnerHandlerCallback innerHandlerCallback = new InnerHandlerCallback();
handler = new Handler(innerHandlerCallback);
}
public void onClick(View view) {
InnerThread thread = new InnerThread();
thread.start();
}
private class InnerHandlerCallback implements Handler.Callback {
public boolean handleMessage(Message msg) {
if (msg.what == MESSAGE_UPDATE_PROGRESS) {
Log.d("Handler", "InnerHandlerCallback.handleMessage->" + MESSAGE_UPDATE_PROGRESS);
progressBar.setProgress(msg.arg1);
textView.setText(msg.arg1 + "/100");
} else if (msg.what == MESSAGE_UPDATE_COMPLETE) {
Log.d("Handler", "InnerHandlerCallback.handleMessage->" + MESSAGE_UPDATE_COMPLETE);
Toast.makeText(MainActivity.this, "更新进度完成", Toast.LENGTH_SHORT).show();
}
return false;
}
}
public static final int MESSAGE_UPDATE_PROGRESS = 999;
public static final int MESSAGE_UPDATE_COMPLETE = 998;
private class InnerThread extends Thread {
public void run() {
for (int i = 0; i <= 100; i++) {
//发消息,通知主线程更新UI
Message msg = Message.obtain();
msg.what = MESSAGE_UPDATE_PROGRESS;
msg.arg1 = i;
handler.sendMessage(msg);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//发出消息,标识更新进度完成
Message msg = Message.obtain();
msg.what = MESSAGE_UPDATE_COMPLETE;
handler.sendMessage(msg);
}
}
}