本文介绍了在多个活动使用的单级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个名为 WebSocketClient ,它提供的功能,打开并连接到的WebSocket,通过WebSocket的发送数据,等等。现在,我想在我的活动中使用这个类。为了被告知从我创建WebSocket的进来的消息的 WebsocketListener 可在 WebSocketClient 所以注册它想通过WebSocket的交流活动将实现此接口并注册。问题是,我怎么可以使用 WebSocketClient 在多个活动?我的第一个想法是贯彻落实 WebSocketClient 类作为辛格尔顿,所以在每一个活动,我能够得到的实例通过 WebSocketClient.getInstance()和自身注册为一个 WebSocketListener 的WebSocketClient。这是实现我想要什么的好方法?所以,如果我在活动A 我称之为 WebSocketClient.getInstance()。寄存器(本),当我切换下一个 b活动我也有叫 WebSocketClient.getInstance()。寄存器(本)这将改变当前监听到当前活动的活动。这样,我能够使用 WebSocketClient 中的每一项活动。

I implemented a class called WebSocketClient which provides the functionality to open and connect to a websocket, send data through the websocket, etc...Now I want to use this class in my Activities. In order to be informed of incomming messages from the websocket i created a WebsocketListener which can be registered in the WebSocketClient so the activity which would like to communicate through the Websocket would implement this interface and register itself. The problem is, how can I use the WebSocketClient in multiple activities? My first idea was to implement the WebSocketClient class as a Singleton, so in each activity I am able to get the instance of the WebSocketClient via WebSocketClient.getInstance() and register itself as a WebSocketListener. Is this a good way of implementing what i want?So if i am in Activity A i would call WebSocketClient.getInstance().register(this), when i switch to the next Activity B I also have to call WebSocketClient.getInstance().register(this) which would change the current listener to the current active activity. This way i am able to use the WebSocketClient in each activity.

将这项工作?没有任何人有一个更好的解决方案?

Will this work? Does anybody have a better solution?

亲切的问候

推荐答案

您可以使用应用程序类,并开始你的 WebSocketClient 类作为一个单身。

You could use the Application class and start your WebSocketClient class as a Singleton.

public class WebSocketClientManager implements OnCloseListener {

 private WeakHashMap<? extends BaseUIListener, Integer> uiListenerss;
 private final static WebSocketClientManager instance;
 static {
    instance = new WebSocketClientManager();
 }

 public static WebSocketClientManager getInstance() {
    return instance;
 }

 @Override
   public void onCloseingTheApp() {
   // do stuff
 }

 // methods
  getListeners 
  setListener
}

您也可以让应用程序类保持所有的 BaseUIListener 。然后在所有的 Activity's onResume()你这样做:

You could also let the Application class hold all BaseUIListener. And then in all your Activity´s onResume() you do like this:

 Application.getInstance().addUIListener(OnSomeChangedListener.class, this);

WebSocketClientManager 时,它有话要说它会做这样的

Your WebSocketClientManager when it has something to say it will do like this

for (OnSomeChangedListener someChangedListener: Application.getInstance().getUIListeners(OnSomeChangedListener.class))
     someChangedListener.OnSomeChanged("This just happend");

有一个非常漂亮的这个位置的例子: xabber应用类

There is a very nice example of this here:xabber Application class

这篇关于在多个活动使用的单级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:17