Android service & application: Architectural question

Hello,

yesterday, I’ve been able to install an OpenFire server and developp a tiny Android application to be able to use it (with asmack of course). Now, before starting my real app, I’m wondering something and I don’t know how to find a precise answer, so I’m posting here

Actually, let’s imagine that I want to build whatsapp (http://en.wikipedia.org/wiki/WhatsApp). I think (correct me if I’m wrong) that this app would be separated in two component: the application to actually chat and a service that would maintain a XMPP connection with the server and would send notifications to the phone when a new message is received.

I see how it would work when the application is not running. The service has the connection, receives messages and sends notifications, but what happened when the user is opening the application itself? The application should have a XMPP connection to be able to receive new messages, but what happen to the connection of the service ? It’s disconnected or this one doesn’t create any notifications because the service “knows” that the application is currently running ? Or is there a way to prioritize (not sure of this word ) connections to say “ok, the connection A is more important than the connection B so all messages will be received on A and not on B”, etc…

To be honest, I don’t really know what is possible and what would be the best option so if someone could give me a hint or maybe a link to an article explaining that, it would be very useful.

Thanks

As far as I know you install one ‘App’ in Android. This App has the UI and it can also start a ‘service’. The service will usually not be terminated and may open the app or send a notification.

So you need inter-process communication to get/send messages via service and to show/enter them in the app. Sounds complicated but it’s not. You can either call methods directly (blocking, not so good) or use an internal queue.

The service will usually not be terminated
Yes, “usually”, Android is free to terminate and reschedule non-foreground services for a restart in certain situations. But you should use non-foreground services for your XMPPTCPConnection. see Home · Flowdalic/asmack Wiki · GitHub reground-Services%3F

and may open the app or send a notification.
It should open an Activity, not an App.

So you need inter-process communication to get/send messages via service

You should design your App so that Service and Activity are components of the same application (and (dalvik) process). This removes the need for inter-process communication (IPC).

but what happened when the user is opening the application itself?

You should design your app so that the user does not open a new application when he is opening the GUI. Instead Service and Activity should be components of the same app.

It’s disconnected or this one doesn’t create any notifications because the service “knows” that the application is currently running ?

Why should the service be killed when an Activiy is displayed? The Service is responsible for manageing the XMPPTCPConnection. Any activity will just interact with the Service.

There are many open source XMPP Android clilents out there. I suggest you have a look at their source to get an idea.

You should design your app so that the user does not open a new application when he is opening the GUI. Instead Service and Activity should be components of the same app.

Currently, if I go in the “applications management” of my phone, I see (for my application) “1 application and 1 service”. But the user can kill my application while my service is still running. Is it what you’re talking about or should I change the architecture of my application?

What I understood is that it is always the service that should maintain the XMPPConnection. What about notifications? Is it managed by the service or by the app?

Actually, I have some difficulties to understand how the service and the application communicate. How the service could communicate with the application if this one is closed?

Thanks for all your answers and sorry if I take time to understand

I see (for my application) “1 application and 1 service”
Does it really say “application” and not “process”?

What about notifications? Is it managed by the service or by the app?
Depends on what you mean with “app”. Android just knows processes and components.

How the service could communicate with the application if this one is closed?
Why is it closed?

I’m sorry, I think I’m very unclear about what I mean. I’ll try to be as clear as possible.

Does it really say “application” and not “process”?

Indeed, it says process. So, currently, my application is composed by some activities (a main one and some other for registration, login, etc…) and a service. This one is just a simple class that inherits from Service. So far, it is a service that launches itself every 5 minutes (because this service is not the chat one but a service that executes a task periodically) but I guess that the service I’ll have to create for the chat will be the same kind except that this service won’t “never stop” (except if Android shut it down).

Depends on what you mean with “app”. Android just knows processes and components.

For me, the app is the graphical interface. On my Galaxy S5, when I press the bottom left key, I see a list of every applications still running (but services are not displayed there). Therefore, my activity is displayed there but not my service. If I kill every apps shown in there, my acticity is killed but my service is still running.

Why is it closed?

Imagine this scenrio.

  • The user starts the application.

  • He chats a bit and then reduces the application.

  • Then he goes in the running application list and kill every running applications.

Here, the service will still listen the XMPP connection but the application (so the main activity) won’t be running anymore. In this case, when a packet is received on the connection, the service displays a notification (I guess). Imagine now that the user opens the application… Now, the service must still listen for packets but when it receives one, it must “tell it” to the application and not display a notification (when you are in a chat, you don’t want to receive a notification… as you are in the chat).

I don’t know if I’m clear but don’t hesitate to correct me on some points if I’m wrong.

EDIT:

Waiting for some news in this topic, I started the mentionned service. So I create a class that extends Service and in it, I created a XMPPTCPConnection. When I reiceved a message, it diplays a notification that opens the application when I click on it. Currently, it opens the home activity but I guess I can pass some information to it for it to open the correct chat window.

Then, here is the question I asked above. If the chat window is open, the service must react differently. Instead of displaying a notification, it must send the new message to the chat window for this one to display it. Moreover, no notification should appear. This is exactly the question I cannot find an answer to. How the service can know that the chat window is opened and can it send “message” to it for the chat window to display them?

Thanks a lot for your help