我正在使用 javafx gluon 和 GCM 开发移动应用程序进行消息传递
当其他设备广播消息时,我想通过调用其控制器来刷新带有传入消息的当前活动视图。
我正在使用Afterburner框架。它还在胶子入门中进行了描述
如何从扩展 MobileApplication 的类中的 getView() 检索视图控制器?
这是我创建的类:
public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG = "NotificationExample";
public MyGCMListenerService() {
}
@Override
public void onMessageReceived(String from, Bundle data) {
String varMessage = data.getString("message");
try {
JSONObject json = new JSONObject(varMessage);
String messageContent = getStringFromJSON(json, "message");
Integer senderId = getIntegerFromJSON(json, "senderId");
String senderName = getStringFromJSON(json, "senderName");
String comId = getStringFromJSON(json, "communityId");
SKSApplication.getInstance().doRefreshMessageUI(messageContent,senderId,senderName,comId );
} catch (JSONException e) {
e.printStackTrace();
}
}}
布局 xml 文件 (directmessage.fxml)
<View xmlns:fx="http://javafx.com/fxml/1" fx:id="directMessageView" prefHeight="600.0" prefWidth="400.0"
xmlns="http://javafx.com/javafx/8.0.40"
fx:controller="com.tenma.mobile.message.directmessage.DirectMessagePresenter">
</View>
DirectMessageView 类扩展了 FXMLView
public class DirectMessageView extends FXMLView {
}
类 DirectMessagePresenter 实现 Initializable
public class DirectMessagePresenter implements Initializable{
public void displayIncomingMessage(String messageContent,Integer senderId,String senderName, String comId){
//save to internal db
// display message
}
}
扩展 MobileAplication 的 SKSAplication 类
public class SKSApplication extends MobileApplication{
private static SKSApplication instance;
public static final String DIRECT_MESSAGE_VIEW = "DIRECT_MESSAGE_VIEW";
public static final String GROUP_MESSAGE_VIEW = "GROUP_MESSAGE_VIEW";
public SKSApplication() {
instance = this;
}
public static SKSApplication getInstance() {
return instance;
}
addViewFactory(HOME_VIEW, () -> {
HomeView homeView = new HomeView();
homePresenter = (HomePresenter) homeView.getPresenter();
return (View) homeView.getView();
});
addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
DirectMessageView directMessageView = new DirectMessageView();
return (View) directMessageView.getView();
});
addViewFactory(GROUP_MESSAGE_VIEW, () -> {
GroupMessageView groupMessageView = new GroupMessageView();
return (View) groupMessageView.getView();
});
public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName, String comId ) {
if (DIRECT_MESSAGE_VIEW.equals(getView().getName())) {
在这里,我尝试从 doRefreshMessageUI 方法中的 getView() 检索直接消息视图,但它不起作用
DirectMessageView view = (DirectMessageView) getView();
DirectMessagePresenter presenter = (DirectMessagePresenter) view.getPresenter();
presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
}
}
}
先感谢您