[英]Using single Class in multiple Activities
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. 我实现了一个名为
WebSocketClient
的类,该类提供了以下功能:打开并连接到websocket,通过websocket发送数据,等等。。。现在,我想在我的Activity中使用该类。 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. 为了通知来自
WebsocketListener
的传入消息,我创建了一个WebsocketListener
,可以在WebSocketClient
进行注册,因此希望通过Websocket通信的活动将实现此接口并进行自身注册。 The problem is, how can I use the WebSocketClient
in multiple activities? 问题是,如何在多个活动中使用
WebSocketClient
? 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
. 我的第一个想法是将
WebSocketClient
类实现为Singleton
,因此在每个活动中,我都可以通过WebSocketClient.getInstance()
获取WebSocketClient的实例并将其注册为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. 因此,如果我处于
Activity A
我将调用WebSocketClient.getInstance().register(this)
,当我切换到下一个Activity B
我也必须调用WebSocketClient.getInstance().register(this)
,这将更改当前侦听器到当前的活动。 This way i am able to use the WebSocketClient
in each activity. 这样,我可以在每个活动中使用
WebSocketClient
。
Will this work? 这样行吗? Does anybody have a better solution?
有人有更好的解决方案吗?
kind regards 亲切的问候
You could use the Application
class and start your WebSocketClient
class as a Singleton. 您可以使用
Application
类并将WebSocketClient
类作为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
}
You could also let the Application
class hold all BaseUIListener
. 您还可以让
Application
类保留所有BaseUIListener
。 And then in all your Activity´s
onResume()
you do like this: 然后,在所有
Activity´s
onResume()
您都这样做:
Application.getInstance().addUIListener(OnSomeChangedListener.class, this);
Your WebSocketClientManager
when it has something to say it will do like this 您的
WebSocketClientManager
有话要说的话,它会像这样
for (OnSomeChangedListener someChangedListener: Application.getInstance().getUIListeners(OnSomeChangedListener.class))
someChangedListener.OnSomeChanged("This just happend");
There is a very nice example of this here: xabber Application class 这里有一个很好的例子: xabber应用程序类
If you store your WebSocketClient object in the Application class you would not have to have Singleton. 如果将WebSocketClient对象存储在Application类中,则不必具有Singleton。 You could just get it from getApplicationContext() method in activities and use its methods as they are.
您可以从活动中的getApplicationContext()方法获取它,并按原样使用它的方法。
That being said, if you will implement Singleton, I would suggest using INSTANCE variable in enum instead of getInstance() method. 话虽如此,如果您将实现Singleton,我建议在枚举中使用INSTANCE变量而不是getInstance()方法。 You can find example at following url: What is the best approach for using an Enum as a singleton in Java?
您可以在以下网址找到示例: 在Java中将枚举用作单例的最佳方法是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.