1. #1
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default Unanswered: Detect if native Android app is running in the background?

    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.

  2. #2
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Answers
    148
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    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.

    http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    Hope this helps.
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  3. #3
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default


    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?

  4. #4
    Sencha - Services Team AndreaCammarata's Avatar
    Join Date
    Jun 2009
    Posts
    1,394
    Answers
    148
    Vote Rating
    22
    AndreaCammarata has a spectacular aura about AndreaCammarata has a spectacular aura about

      0  

    Default


    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.
    Sencha Inc
    Andrea Cammarata, Solutions Engineer
    CEO at SIMACS

    @AndreaCammarata
    www.andreacammarata.com
    github: https://github.com/AndreaCammarata

    TUX components bundle for Sencha Touch 2.x.x
    http://www.tux-components.com/


  5. #5
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default


    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:

    Code:
    Ext.device.Geolocation.clearWatch()
    If the user comes back to the app I restart the geolocation update again.

    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. #6
    Sencha User
    Join Date
    Aug 2010
    Posts
    303
    Answers
    3
    Vote Rating
    3
    steve1964 is on a distinguished road

      2  

    Default


    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
    http://docs.phonegap.com/en/2.3.0/co...md.html#Events

  7. #7
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default


    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.

  8. #8
    Sencha User
    Join Date
    Jul 2012
    Posts
    26
    Answers
    2
    Vote Rating
    0
    squistbe is on a distinguished road

      0  

    Default


    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.