Red-Folder / bgs-core

Core code for the Cordova Background Service
Other
236 stars 105 forks source link

Code of background service #55

Closed GustavoCostaW closed 8 years ago

GustavoCostaW commented 8 years ago

Why the code of background service is in Java and not in Javascript?

In any callback Javascript for start service? or have limitations of Android? It's kill the WebView or something like this?

For example in iOS the code of background fetch works in WebView (Javascript)

https://github.com/christocracy/cordova-plugin-background-fetch (check sample)

Thx

Red-Folder commented 8 years ago

Its to do with the Android application lifecycle.

To be able to run background logic (you're app is closed, phone is "asleep", etc) you need to run an Android Background Service.

A Background Service is, to Android, a different fish than an App. In handles its lifecycle completely differently.

The Background Services is, by nature, Java. For it to have access to JavaScript it would need access to something that can interpret and run JavaScript. When the Cordova/ Phonegap App is running, that can provide the JavaScript environment - thus you can signal from the background service to the app to trigger JavaScript logic.

However, when the app closes, the Background Service doesn't have access to a JavaScript environment. Thus its logic has to be in Java.

In theory, it should be possible for the Background Service to create it's own JavaScript environment (maybe by trying to run the WebView in some form of headless manner).

I did ask for ideas on how to do this a few years back - but I drew a blank. Seems like a good idea, but no-one knew how to do it.

It probably is possible to enable JavaScript to run from within the Background Service - but to be honest, I have no interest in looking into that or indeed progressing this plugin at this time.

I'd advise you to see this as a good excuse to learn a bit of Java & Android Native programming ;)