Reputation: 14158
I have a service. I create a Thread
for using socket.io-java-client
library on separate thread in this Service.
I keep thread with following way:
@Override
public void run() {
while (canLiveThread) {
// keep thread
}
}
And this way fully wrong yes? To fix this solution I need find answer to following questions:
1. Are need to me create separately thread to use socket.io-java-client library in Service? Or I can use this library without thread, simply implement socket.io-java-client library in Service?
2. If need create thread to use this library, then how to manage thread with right way to not draining battery in background Service?
If anybody have any Solution, please answer to this question ...
Thanks in Advance!
Upvotes: 4
Views: 1126
Reputation: 12002
Actually, I don't know is my way to use socket-io-client
right, but I try to be completely clear.
As I know socket-io-client
is already using background threads to connect, send, receive events.
FIRST WAY
I think that it's not what you want, but I show this variant as possible.
So, firstly, I've created a SocketClient
, like this:
public class SocketClient implements IOCallback {
private SocketIO socket;
private Context context; // for some cases (i.e. broadcasting via LocalBroadcastManager).
public SocketClient(Context context) {
this.context = context;
}
public synchronized void connect() {
if (socket == null || !socket.isConnected()) {
Properties properties = new Properties();
// setting your properties...
try {
socket = new SocketIO("http://your.address", properties);
socket.connect(this);
} catch (MalformedURLException e) {
Log.e("SocketClientException", e.getMessage());
}
}
}
@Override
public void onDisconnect() {
}
@Override
public void onConnect() {
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
}
@Override
public void onMessage(JsonElement json, IOAcknowledge ack) {
}
@Override
public void on(String event, IOAcknowledge ack, JsonElement... args) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("action_on"));
}
@Override
public void onError(SocketIOException socketIOException) {
}
}
Then I've created a Singleton that holds our SocketClient
:
public class API {
private static API instance;
private SocketClient client;
private API() {
}
public static API getInstance() {
if (instance == null) {
instance = new API();
}
return instance;
}
public void init(Context context) {
client = new SocketClient(context);
}
public SocketClient getSocketIO() {
return client;
}
}
And then in the Service
class onCreate()
method I'am simply calling my singleton:
@Override
public void onCreate() {
super.onCreate();
Log.i("SocketService", "onCreate");
API.getInstance().init(getApplicationContext());
API.getSocketIO().connect();
}
After that the socket will be alive as long as a Service
, so we will be able to receive, send socket commands in background via our singleton from anywhere in our application. We can send received data via LocalBroadcastManager
from on
, onMessage
methods in SocketClient
.
SECOND WAY
Let's try to implement a SocketService
:
public class SocketService extends Service implements IOCallback {
private SocketIO socket;
private synchronized void connect() {
if (socket == null || !socket.isConnected()) {
Properties properties = new Properties();
// setting your properties...
try {
socket = new SocketIO("http://your.address", properties);
socket.connect(this);
} catch (MalformedURLException e) {
Log.e("SocketClientException", e.getMessage());
}
}
}
@Override
public void onCreate() {
super.onCreate();
connect();
}
@Override
public void onDisconnect() {
}
@Override
public void onConnect() {
}
@Override
public void onMessage(String data, IOAcknowledge ack) {
}
@Override
public void onMessage(JsonElement json, IOAcknowledge ack) {
}
@Override
public void on(String event, IOAcknowledge ack, JsonElement... args) {
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("action_on"));
}
@Override
public void onError(SocketIOException socketIOException) {
}
}
That's all. You can startService from your activity and communicate via broadcasts or messagehandlers. I have never tried the second way, but I think it should work like a charm.
CONCLUSION
No one of this methods uses separately background threads, and it's working for me, I have three apps based on the first way, and all of them are working in background without any supporting things. Hope the answer will be helpful for all interesting in SocketIO
usage :)
Upvotes: 1