Android service - how exactly it works
this is a question I should have had probably asked before I did it. But I didn't know it before I did it. :-)
I implemented a service for Android version of my application (a music player). The motivation was that sometimes my player was killed by the system when it was running in the background. But I found out that in fact it caused new problems and didn't solve this one.
Everything woks as expected in the beginning. But then I picked my app from the list of previously run applications and I found that I can not start the playback. I do believe it is because the service is not running anymore. And here come the questions:
- How can I make a persistent service that "never" gets killed (I know there is no never on Android, but get to it as close as possible). Such services usually have an icon in the notification bar. Is this possible with Qt?
- What happens if the service gets killed? Can I get a notification about it? How do I restart it?
- What happens if my GUI application is killed and the service is still running? Can I recognize this? Can I connect to it again?
- How do I intentionally stop the service if I don't need it (= playback is stopped)?
- Do I have to stop the service when quitting my application?
Many thanks for answers to any of my questions.
I haven't implemented a service in Android however, Qt now has support for them. You can start from the Android Services chapter in Qt's documentation.
Hope it helps
For me it is too interest, and I too have an additional questions:
Do we need to create a two applications - one of them is GUI, and other of them - is service, which duplicate a functionality of each other?
How then they will know, what currently need to start a service, and when - start an GUI application?
Or, We just create an one GUI application, which just contains appropriate records in the manifest file, to work as service?
I have read many infos, but did not understand how it work.. What is an idea? E.g. let's assume on the music-player example. Could someone explain: what steps are necessary to create e.g. the music-playes application, which has a GUI and which should work in background?
@SGaist I know, I was watching the development of this feature closely. Now I already have implemented it. It just doesn't work as I expected. And I don't know if it is a limitation of current Qt's implementation or if I have to do something differently.
- Your application runs either as a service or as a GUI application.
- You create an application and an associated service. Each of them run in it's own process.
My music application now has a service and runs even in background. But this was possible even without a service. I was hoping that the service will be more resistant and the system won't kill it. But it doesn't seem to be that case. So it doesn't make much sense to create the service then.
@kuzulis You should check this guide. It explains it quite well. Basically you have 2 options:
I already read it, but still there are unclear moments.
Your application runs either as a service or as a GUI application.
You create an application and an associated service. Each of them run in it's own process.
F.e. I use one *.so file as a GUI and as a service... How then the system know, that now need to run a service instead of GUI? Is it does automatically? Or, when I start a GUI application manually, then it automatically launch and a service too? I.e. will be running two processes simultaneous? If so, then interfere they with each other at the same time (do they same work?)?
@kuzulis Sorry, I have no idea how it works if you have one .so file. I think in this case you run the same application either as an activity or as a service. But I might be wrong.
There is really a huge lack of information and documentation regarding Android services in Qt. :-(
This works for me. Now I have a service which remains running even if I quit the GUI part. And it has an icon in notification area.
Now I need to find out following things:
- How to start again the GUI application If I click the notification.
- How to properly close the service if the playback is stopped.
- Enable Android (Java) code in service.