Executing Javascript in background using Phonegap
Asked Answered
A

2

9

I've created an application that will need to fire Javascript events every "X" minutes. I'm using phone gap build to deploy on Android/iPhone/Blackberry, but I'm of course stuck, as the Javascript timer doesn't fire in the background (when they minimize the app). I was wondering if Phonegap had some sort of ability to fire Javascript events while the app is running in the background.

Or is there ANY way to accomplish this easily across these major phones? I just need to fire an ajax call with the geolocation info from the phones.

Amador answered 28/9, 2011 at 15:41 Comment(4)
My experience with phone gap is that it is very limited. This is absolutely possible, but you will have to add native code to do what you want, for each platform.Establishmentarian
I do realize that doing this natively is more than possible, but it would be a big pain to go that route for this single problem I have... the rest of my app works great on the phones. I'm hoping someone has a "OH, just do this!!!" moment for me... These stackoverflow programmers are a smart group right ;)Amador
I ended up learning objective C and writing the app. It's available for download at imnearby.caAmador
Phonegap is interesting in theory but for actual commercial applications, it is very limiting and offers a slightly poor user experience. I myself also ended up writing native applications. Welcome to the dark side.Establishmentarian
F
2

Yes you can do it, there's a PhoneGap plugin that does exactly what you need:

PhoneGap + Background.js https://github.com/jocull/phonegap-backgroundjs

Be careful configuring your config.xml

Fencing answered 15/12, 2015 at 18:36 Comment(1)
You can also use this to force a background mode: github.com/katzer/cordova-plugin-background-modeFencing
H
1

Currently, phone gap doesn't have this as an option. There are many requests out there for this sort of thing, but I think people are getting tripped up by the TOS for background events for iOS. Regardless, a phone gap plugin would do it as @Chase mentioned, but of course that requires native code writing.

I'm assuming you're wanting to push geo locations every x seconds/minutes so that your server knows where the phone is at any point in time (as long as the app is open or minimized)? That's one limitation of Loopt, is that it can't grab those locations in the background - hence the whole 'ping' action in the app.

Harrovian answered 30/9, 2011 at 19:2 Comment(2)
I ended up throwing in the towel and going native. I'm learning Objective C now, and it's not too tough to wrap your head around. In the end, the overall user experience is better when going native, and learning new languages is always a plus... so thanks for your input, I'm sure it will be valuable to others in my position!Amador
So, it turns out learning objective C isn't too tough. There are some great resources on iTunesU from Standford University that teach you objective C step by step. I'd recommend it to everybody. Also, if anyone is interested in seeing the application I made, you can check it out at imnearby.caAmador

© 2022 - 2024 — McMap. All rights reserved.