android SDK核心功能 — 消息传递与界面刷新
最后更新于:2022-04-01 10:06:05
今天来分享一下 项目中view刷新相关的核心功能。
android里线程之间消息的传递都是借助Handler来实现的
用到的场景主要是是Activity、Fragment、Application 、Service、Thread之间的消息互相传递
此实现必须借助抽象类来实现即
BaseActivity BaseFragment PhoebeApp (现在不讨论Service之间的消息传递)
先来看一下BaseActivity的实现
~~~
public abstract class BaseActivity extends Activity implements Handler.Callback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMgr.push(this);
findViewById();
}
@Override
public boolean handleMessage(Message msg) {
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityMgr.remove(this);
}
}
~~~
其中BaseFragment的实现类似。省略
接下来看一下核心类
~~~
/**
* app中用于UI/非UI线程通信的核心工具类<br>
* 可以再"UI/非UI线程"往"UI线程"中发信息, 最终接收方为 {@link BaseActivity} {@link BaseFragment} {@link PhoebeApp}
*
* @author coffee<br>
* 2015-12-26下午4:36:26
*/
public class HandlerMgr {
public static void sendMessage(int what) {
int delayMillis = 0;
final Message msg = Message.obtain();
msg.what = what;
sendMessage(msg, delayMillis);
}
/**
* 将消息发往Application、Activity、Fragment
*
* @param orig
* @param delayMillis
*/
public static void sendMessage(Message orig, int delayMillis) {
// 发往application
Message appMessage = Message.obtain(orig);
PhoebeApp.getHandler().sendMessage(appMessage);
// 发往Activity
ArrayList<?> activities = ActivityMgr.getAllActivities();
for (final Object activity : activities) {
boolean result = handle(activity, orig, delayMillis);
if (result == true) {
break;// 停止继续分发该Message
} else {
continue;
}
}
// 发往Fragment
// ArrayList<?> fragments = FragmentMgr.getAllFragments();
// for (final Object fragment : fragments) {
// boolean result = handle(fragment, orig, delayMillis);
// if (result == true) {
// break;// 停止继续分发该Message
// } else {
// continue;
// }
// }
}
<span style="white-space:pre"> </span>
private static boolean handle(final Object activityOrFragment, Message orig, int delayMillis) {
final Message msg = Message.obtain(orig);
if (delayMillis == 0) {
boolean result = false;
if (activityOrFragment instanceof Handler.Callback) {
((Handler.Callback) activityOrFragment).handleMessage(msg);
}
return result;
} else {
PhoebeApp.getHandler().postDelayed(new Runnable() {
@Override
public void run() {
if (activityOrFragment instanceof Handler.Callback) {
((Handler.Callback) activityOrFragment).handleMessage(msg);
}
}
}, delayMillis);
}
return false;
}
}
~~~
如果Thread发送的http请求 。请求完成以后
~~~
public void run(){
// ...处理网络请求
Message msg = Message.obtain();
msg.what = 123;
msg.obj = "{json字符串}";
HandlerMgr.sendMessage(msg, 0);
}
~~~
这个时候如果你Activity需要接收返回的结果
~~~
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 123:
String json = msg.obj + "";
//处理json数据
break;
}
}
~~~
如果你想在多个Activity处理该http返回的结果。只需要在Activity的handleMessage中加一个case就行了
如果需要在多个Activity中接收,这里需要注意一下handlerMessage的返回值
如果返回true 只会有一个Activity 接收到。剩下的都接收不到了。
~~~
for (final Object activity : activities) {
boolean result = handle(activity, orig, delayMillis);
if (result == true) {
break;// 停止继续分发该Message
} else {
continue;
}
}
~~~