Pregunta

On my Android App, I'm implementing SignalR connection (https://github.com/erizet/SignalA) to connect to a Hub server to send requests and receive responses.

a sample of my code is as follows:

signalAConnection = new com.zsoft.SignalA.Connection(Constants.getHubUrl(), this, new LongPollingTransport())
{
    @Override
    public void OnError(Exception exception)
    {
    }

    @Override
    public void OnMessage(String message)
    {
    }

    @Override
    public void OnStateChanged(StateBase oldState, StateBase newState)
    {
    }
};

if (signalAConnection != null)
    signalAConnection.Start();

There's also the sending bit

signalAConnection.Send(hubMessageJson, new SendCallback()
{
    public void OnError(Exception ex)
    {
    }

    public void OnSent(CharSequence message)
    {
    }
});

The sending and receiving will occur across activites, and some responses will be sent at random times regardless of the activity, also, the connection should be opened as long as the app is running (even if the app is running in the background) that's why I wish to implement the signalA connection as a background service

The question is should I implement it as:

1 - a Service (http://developer.android.com/reference/android/app/Service.html)

OR

2 - an Intent Service (http://developer.android.com/training/run-background-service/create-service.html)

Keeping in mind that I will need to send strings to the service and get response strings from the service.

I would be most grateful if someone would show me how to implement this kind of connection in code as a background service/intentservice.

Thanks for reading.

UPDATE:

Please see this demo activity made by the developer as how he implemented SignalA https://github.com/erizet/SignalA/blob/master/Demo/src/com/zsoft/SignalADemo/DemoActivity.java

The problem is AQuery (which I know nothing about) is being used in this demo activity. Does AQuery run in the background all the time ? The problem is, the latest update on SignalA mentions the following

I have changed the transport. LongPolling now uses basic-http-client instead of Aquery for http communication. I've removed all dependencies on Aquery.

Hence I'm not sure whether I should follow this demo activity or not

Update 2:

This is the thing that is confusing me most in the IntentService, the OnHandleIntent method calls stopSelf after it finishes its tasks, when I actually want the code in the IntentService to keep running all the time

protected abstract void onHandleIntent (Intent intent) Added in API level 3 This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call stopSelf().

¿Fue útil?

Solución

SignalA is running on the thread that creates and starts the connection, but all network access is done in the background. The remaining work on the starting thread is really lightweight, hence its perfectly ok to do it on the UI tread.

To answer your question, you need to have a thread running the signala connection. Therefore I think a Service is the best choice since SignalA need to be running all the time.

Regarding Aquery and the demo project. I removed all dependencies to Aquery in the libraries, not in the Demo. To be clear, you don't need Aquery to run SignalA.

Otros consejos

In my case, what I wanted was a Service not an Intent Service, since I wanted something that would keep running until the app closes

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top