Handler跨activity传递
程序员文章站
2022-07-14 16:43:52
...
1.在MyApplication中添加Handler的set and get
public class MyApplication extends Application {
public static MyApplication instance;
private Handler mHandler;
public static MyApplication getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public Handler getmHandler() {
return mHandler;
}
public void setmHandler(Handler mHandler) {
this.mHandler = mHandler;
}
}
2.添加name属性
android:name=".utils.MyApplication"
3.在一个类中setHandler
public Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
// 处理逻辑
return false;
}
});
...
...
...
final MyApplication instance = MyApplication.getInstance();
instance.setmHandler(handler);
4.在另一个类中getHandler然后send
Message getChatMessage = new Message();
getChatMessage.what = ChatActivity.MSG_REC2;
getChatMessage.obj = o;
final MyApplication instance = MyApplication.getInstance();
instance.getmHandler().sendMessage(getChatMessage);
to be continued
上一篇: Handler的使用
推荐阅读
-
关于Android Activity之间传递数据的6种方式
-
Spring之跨重定向请求传递数据
-
Android Activity之间的数据传递方法总结
-
Android实现音乐播放进度条传递信息的两种方式(在service和activity中)
-
Android Activity之间传递图片(Bitmap)的方法
-
Android中Service实时向Activity传递数据实例分析
-
HTML5实战与剖析之跨文档消息传递(iframe传递信息)
-
Android开发--Service和Activity通过广播传递消息
-
html5 postMessage解决跨域、跨窗口消息传递方案
-
Android Studio 之 Android消息机制 之简单Demo --- 使用Handler和Message类来完成消息传递