5 Feb 2013 3:23 AM #1
Unanswered: Detect if native Android app is running in the background?
A big difference on Android vs iOS is that apps on Android is still running in the background if the user goes back to the homescreen.
On an app I'm building I'm frequently getting geolocation updates to visualize distance to different places. The problem is, when the user closes the app, the app is still running in the background, and the gps is draining the battery.
Is it possible in Sencha Touch to detect if an Android app (packaged as native) is running, or has been closed and is running in the background?
I would like to stop the geolocation watchposition when the app is running in the background.
5 Feb 2013 5:05 AM #2
That's not possible to understand from the Sencha Touch side, but it's something you should handle on the native app. If you are using PhoneGap you should have no problems to create a plugin to handle this situation.
Let me point you to a useful link where you will find all the info you need to understand Android apps lifecycle.
Hope this helps.
5 Feb 2013 5:38 AM #3
Thanks, I've seen that page when searching for info on this subject.
I'm not using PhoneGap. Instead I'm using Sencha Cmd to package the app into an Android app. I guess it is not possible to detect the active state by using Sencha Cmd? I think it would be a great feature to implement an active state listener in Sencha Cmd. Maybe something you would consider?
I'm thinking of a way to work around this limitation. Maybe by listening on tap events on the main viewport. If no tap events has been made for, lets say, 5 minutes. Then the app is probably inactive, and I could disable the geolocation update. And if a new tap event occurs, then restart the geolocation update.
What do you think. Is there a better solution?
5 Feb 2013 5:52 AM #4
Yeah, it's still not possible to get more info about the app state when it is wrapped using Sencha Cmd, but I'm sure it will in the future.
About your solution that could be a good idea.
Unfortunately you need to elaborate the best solution that fit your app needs without compromising the user experience and that it's a good compromise.
6 Feb 2013 2:31 AM #5
I implemented a solution where I listen on tap events on the Ext.Viewport to be able to know if the app is active or not. If the user hasn't been using the app for two minutes, then I stop the gps updates by calling this code:
This seems to work as intended. No geolocation update events are fired in the app. So the geolocation seems to be off. But, the GPS symbol (To the top left in the statusbar) on Android does not disappear. This makes me wonder if the GPS is still active, even though I called the clearWatch() function to stop GPS updates.
Is there a way to stop the GPS completely within the app? Or do I not have to worry about this? It is the battery performance on Android that is my concern here.
6 Feb 2013 5:48 AM #6
Hi, for those having the same problem, if you use Phonegap you can listen for 2 events:
"pause" thats mean the app is in the background
"resume" the app is active again
6 Feb 2013 5:54 AM #7
Cool, thanks. Looks like Phonegap is a better way to package native Android apps than the Sencha packager. Maybe I should switch to Phonegap from Sencha Packager.
10 Apr 2013 12:07 PM #8
I use Sencha Touch wrapped in PhoneGap. Sencha Touch is still an excellent framework and with PhoneGap you will have even more functionality with you app especially for accessing device functionality. I use the pause and resume events all the time.