1.handler作用:
//2种创建消息方法
//1.通过handler实例获取
Handler handler =newHandler();
Message message=handler.obtainMessage();
//2.通过Message获取
Message message=Message.obtain();
//源码中第一种获取方式其实也是内部调用了第二种:
publicfinalMessageobtainMessage(){
returnMessage.obtain(this);
}
不建议直接new Message,Message内部保存了一个缓存的消息池,我们可以用obtain从缓存池获得一个消息,Message使用完后系统会调用recycle回收,如果自己new很多Message,每次使用完后系统放入缓存池,会占用很多内存的。
//传递的数据
Bundle bundle =newBundle();
bundle.putString("msg","传递我这个消息");
//发送数据
Message message = Message.obtain();
message.setData(bundle);//message.obj=bundle 传值也行
message.what =0x11;
handler.sendMessage(message);
//数据的接收
finalHandler handler =newHandler() {
@Override
publicvoidhandleMessage(Message msg){
super.handleMessage(msg);
if(msg.what ==0x11) {
Bundle bundle = msg.getData();
String date = bundle.getString("msg");
}
}
};
//创建handler
finalHandler handler =newHandler() {
@Override
publicvoidhandleMessage(Message msg){
super.handleMessage(msg);
if(msg.what ==0x11) {
//更新ui
......
}
}
};
newThread(newRunnable() {
@Override
publicvoidrun(){
//FIXME 这里直接更新ui是不行的
//还有其他更新ui方式,runOnUiThread()等
message.what =0x11;
handler.sendMessage(message);
}
}).start();
//消息
Message message = Message.obtain();
//发送消息
newHandler().sendMessage(message);
//延时1s发送消息
newHandler().sendMessageDelayed(message,1000);
//发送带标记的消息(内部创建了message,并设置msg.what = 0x1)
newHandler().sendEmptyMessage(0x1);
//延时1s发送带标记的消息
newHandler().sendEmptyMessageDelayed(0x1,1000);
//延时1秒发送消息(第二个参数为:相对系统开机时间的绝对时间,而SystemClock.uptimeMillis()是当前开机时间)
newHandler().sendMessageAtTime(message, SystemClock.uptimeMillis() +1000);
//避免内存泄露的方法:
//移除标记为0x1的消息
newHandler().removeMessages(0x1);
//移除回调的消息
newHandler().removeCallbacks(Runnable);
//移除回调和所有message
newHandler().removeCallbacksAndMessages(null);
1)handler怎么使用会产生内存泄露?
publicclassMainActivityextendsAppCompatActivity{
finalHandler handler =newHandler() {
@Override
publicvoidhandleMessage(Message msg){
super.handleMessage(msg);
......
}
};
@Override
protectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//activity被执行时,被延迟的这个消息存于主线程消息队列中1分钟,
//此消息包含handler引用,而handler由匿名内部类创建,持有activity引用,
//activity便不能正常销毁,从而泄露
handler.postDelayed(newRunnable() {
@Override
publicvoidrun(){
......
}
},1000*60);
}
}
publicclassMainActivityextendsAppCompatActivity{
//创建静态内部类
privatestaticclassMyHandlerextendsHandler{
//持有弱引用MainActivity,GC回收时会被回收掉.
privatefinalWeakReference mAct;
publicMyHandler(MainActivity mainActivity){
mAct =newWeakReference(mainActivity);
}
@Override
publicvoidhandleMessage(Message msg){
MainActivity mainAct=mAct.get();
super.handleMessage(msg);
if(mainAct!=null){
//执行业务逻辑
}
}
}
privatestaticfinalRunnable myRunnable =newRunnable() {
@Override
publicvoidrun(){
//执行我们的业务逻辑
}
};
@Override
protectedvoidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyHandler myHandler=newMyHandler(this);
//延迟5分钟后发送
myHandler.postDelayed(myRunnable,1000*60*5);
}
}
a)Handler.post(Runnable)其实就是生成一个what为0的Message,调用
myHandler.removeMessages(0);
会使runnable任务从消息队列中清除。