QApplication main loop doesn't work when in background on Android
-
Hi all,
According to this post and discussion here, main event-loop doesn't work when phone is locked, since Qt 5.3 upwards. And this is feature not bug.
My problem is that:
I need to gather GPS position in my app, even when not in active mode, so what I need to do, I have a few questions. I haven't tested this issue on iOS and Windows mobile, but I suppose Qt main event-loop will behave the same. So what options do I have?- I have just now created a new thread and moved class with QTimer into it and it works when app is inactive ;)
- There is something also like Android Services mentioned in discussion here but to be honest this looks like far more complicated.
So in essence, I need also network connectivity, and a few other things, should I move everything not related to GUI to separate thread and it will work ? Network especially. I have event filter installed to tell me when app is in active state and then I can update gui.
Best Regards
Marek -
@Marek said in QApplication main loop doesn't work when in background on Android:
There is something also like Android Services mentioned in discussion here but to be honest this looks like far more complicated.
but thats the way to go for such tasks.
It's the nature of Android, that the app "stops" in the background. -
Probably yes, but I had impression that Android Service is not yet quite ready. In the discussion I mentioned someone asked 13 days ago if anyone had implemented Android Service along with Qt 5.7 app and that the sample project would be very useful.
So the question is:
can I get away with just the separate thread for GPS and network connectivity ?Best Regards
Marek -
@Marek said in QApplication main loop doesn't work when in background on Android:
can I get away with just the separate thread for GPS and network connectivity ?
as i already said...no
As soon as the app goes into the background you its undeterminated what happens. Your app might even get removed from memory when the OS needs to.
Services are designed for background processing -
@raven-worx said in QApplication main loop doesn't work when in background on Android:
Your app might even get removed from memory when the OS needs to.
This I know.
However, google maps works in my phone when it is locked, not refreshing the screen but getting data from GPS it will be closed when system needs more resources.But if the Android Service is a way to go, is there a working example for the app using service ?
And does iOS and Windows Mobile behave in the same way. I read somewhere that in Windows Mobile there shouldn't even be "exit" button in application, as it is system responsibility to close the app.
Best Regards
Marek -
@Marek said in QApplication main loop doesn't work when in background on Android:
But if the Android Service is a way to go, is there a working example for the app using service ?
And does iOS and Windows Mobile behave in the same way
Can't answer that, since i haven't done any work on these platforms yet.
-
Hi
Thanks for example it runs on my Android phone, and it is running in app settings when I close the app.
I have some questions, forgive me my ignorance, I haven't worked with services yet.
After reading this- My app and service is in fact one application, signle .so file ? Part of which runs as a service and part as an application (activity)? In main.cpp there is:
if (QCoreApplication::arguments().count() > 1){ qDebug() << "I am the service"; } else{ qDebug() << "I am the application";
I can see only
source/cpp/main.cpp:19 (int main(int, char**)): I am the application
Is this due to debugger not following service? So, in fact application is started twice? Once as the activity and then as the service?
Should I put part of service code and activity code inside proper main.cpp part?-
"A service runs in the same process as the application in which it is declared and in the main thread of that application, by default" - so I assume that main thread is running even if main event-loop does not ? or QtService creates its own thread?
-
I assume I need "Started" type service as it will run after app is closed by system, until I close service or it closes itself
-
How to exchange data between service and activity? on Android page they say about IPC, but if it is single app...
-
How to stop activity when user chooses to stop application?
Best Regards
Marek -
Hi all
Threads won't work for me, I had to learn this the hard way.
I have rewritten app using separate threads, made thread synchrnization, and it works, even when phone is locked, so great!
No, not really... main thread stops when app is in background, worker works, as long as, I have USB cable connected to the phone, when I disconnect the cable, worker thread continues to work for about 10-15 seconds and also goes to sleep, what a cruel experience.
I'm using Qt 5.7
Can someone give me more directions about Android Service inQt ?Best Regards
Marek -