Hybrid App Question

I have made a browser based web app that sends out the user's lat and lng every 10 seconds. When the page or the browser loses focus, it stops running and the location no longer gets sent out. Is there any way to keep my app running when the browser loses focus? If not, is it possible to create a hybrid app that launches the browser and the page we want, and since it is part native, could it run in the background if the web page/browser loses focus? Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rafael LepkoskiSoftware DeveloperCommented:
How exactly are you getting the user location? Could you provide some code?
BobHavertyComhAuthor Commented:
Hi Rafael, thanks for your response. Here's how I'm doing it.

This gets the position if it has changed.
function watchCurrentPosition() {
    var positionTimer = navigator.geolocation.watchPosition(function (position) {
            setMarkerPosition(currentPositionMarker, position);
            lat = position.coords.latitude;
            lng = position.coords.longitude;
            if (markers.length > 1) {
                for (i = 0; i < markers.length; i++) {
            else {
                map.panTo(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
        function geo_error(error) {
            switch(error.code) {
                /*case error.TIMEOUT:
                    alert('Geolocation Timeout');
                case error.POSITION_UNAVAILABLE:
                    alert('Geolocation Position unavailable');
                case error.PERMISSION_DENIED:
                    alert('Geolocation Permission denied');
                    alert('Geolocation returned an unknown error code: ' + error.code);*/
            enableHighAccuracy: true,
            maximumAge: 10000,
            timeout: 10000

Open in new window

lat and lng are global variables whose values keep getting set by the code above. Then, every 10 seconds, it sends out whatever value lat and lng contain at that moment to an aspx page, which takes that and writes to a database.

    setInterval(function () { $.get(encodeURI("latlng.aspx?lat=" + lat + "&lng=" + lng)); }, 10000);

Open in new window

This all works, but if someone brings up certain other apps to the foreground, it is possible that this will stop sending out it's location because it stops executing if the phone decides to wipe that process out. I've seen this happen and I'm not sure why, or I would like to understand why better. I suppose this really comes down to how the phone treats the browser app itself, and perhaps I would have to have a native app that has a webview in it that displays the web page. This way, if it is native, I could control the pause, resume etc....events.

However, I am also trying the trick where if you have this meta data tag on your page,

<meta name="mobile-web-app-capable" content="yes">

and when you select "save to homescreen", the Droid not only gives it full screen when you click on the shortcut you created, it also appears to treat it as an app in terms of persistence, where I have seen it still running in the background, whereas if someone just launched the browser and typed in the url, it would not persist the same way when it falls out of focus.
Rafael LepkoskiSoftware DeveloperCommented:
For what I understood, it's not a hybrid app like the title is saying, right? Be more careful specifying your quesetion's title next time.
It seems a good feature to prevent JavaScript execution while your page is in background. Keep in mind that mobile devices have limited resources (battery, RAM and CPU) and it would be terrible if the browser let any arbitrary page to keep consuming the device's resources without any concern.
If you build a hybrid or native Android app, and you really need to keep running some task while in background, you can use PowerManager.WakeLock.
But remember that "with great power comes great responsibility" so use this wisely.

If you need some example of how to use WakeLock, please ask :)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

BobHavertyComhAuthor Commented:
I really appreciate your opinion and advice. So what it comes down to is that if I want to keep most of my web code, but I want access to things like Wakelock or other things to make the execution persist, I need to create a native app with a webview control on it that acts like a browser. Then, since it's technically a native app, even though the only thing it does is display a web page, I can take advantage of the phone's features such as wakelock, and onpause, onresume events etc....

I thought that this was essentially what a hybrid app is. So if you didn't want to write a whole bunch of native code for each platform, and instead wanted a web app that can work across platforms, is this what you would do? Also, would a webpage being viewed inside of a native app webview be expected to behave the same as if you were viewing inside of the phone's chrome or ff browser or are there known problems with this?
Rafael LepkoskiSoftware DeveloperCommented:
A hybrid app is one that runs native code side by side with a webview.
If you need to use low level features you need to run native code. One approach you can use to achieve this is to build the major part of your code in JavaScript/HTML and for specific features with native code. If you're gonna to this way, I can recommend you two alternatives: PhoneGap and Titanium.

Using PhoneGap you should create a plugin to use native code and call that plugin in JavaScript. Each platform (Android, iOS etc.) must have it's specific plugin.

I never worked with Titanium, but it promisses that you can write your entire app with just JavaScript and then it translates everything to native code when you build the app. I think it's worth a shot. Altough I suspect that maybe it would be easier for you to use PhoneGap, since you are wanting to deal with a feature that are singular to each platform (like Android's WakeLock).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
Thank you Rafael, that was extremely informative.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.