74

I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?

Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6 So that is all I have right now.

Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.

EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?

Thanks

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Jonovono
  • 3,437
  • 5
  • 42
  • 64

4 Answers4

55

No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.

The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.

You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.

If you really need a service you have to go native.

Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):

  1. visible
  2. invisible but still loaded
  3. not loaded

I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.

When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.

If you don't want this (if you really want a background service), you have to provide a native implementation.

ChrLipp
  • 15,526
  • 10
  • 75
  • 107
  • 3
    Thanks. So if I am understanding, I could create a service that calls a javascript callback? For example, I could have a service in Java code that when it receives a text message it calls a javascript callback function to execute some JS code? That would be perfect. I found this plugin that seems to create a service and call Js code: https://github.com/awysocki/C2DM-PhoneGap – Jonovono May 03 '12 at 15:54
  • Awesome this looks good. Thanks! So if I press the back button when the app is running it should still write to the console when it gets an incoming text? It doesn't seem to? – Jonovono May 04 '12 at 22:40
  • 1
    No, because I deregister when the activity is closed. For your requirement we have to wake the activity per intent and we do not need a plugin. But I am not able to provide a sample the next two days. – ChrLipp May 05 '12 at 08:09
  • hmm ok thanks. So it shouldn't be that hard? It seems to work great I just need it to also execute js code. – Jonovono May 05 '12 at 18:39
  • 1
    Looks like your github link in "Update 2" is outdated. – Mike N Feb 23 '16 at 12:24
  • Thanks, I removed this link. – ChrLipp Feb 25 '16 at 15:27
  • Guys, what about using using **SharedWorkers**? – deostroll Jan 27 '18 at 06:51
9

There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.

It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though

I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful. You would still need to rewrite your JS code in Java though. Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)

Hope that could be useful to some people visiting this page.

Bruno
  • 196
  • 1
  • 7
4

YES, and it is very simple... just install the plugin backgroundJS:

https://build.phonegap.com/plugins/430

It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!

Michel Tobon
  • 81
  • 1
  • 5
  • 1
    Unfortunately it says "Platform(s): iOS" so it won't work for Android :( – s.krueger Mar 19 '14 at 12:19
  • 1
    the above plugin is not running infinitely in my case, around the 3 minutes, the background functions stops. I tried to set the `SetBackgroundSeconds` method to around 300 seconds, but didn't work either, the console stop logging around 180 seconds. – Juan Carlos Alpizar Chinchilla May 23 '14 at 17:24
  • @Juan Carlos... to me it works for around 6 hours, then it messes up, it's important that you have activities working in your application... I think that does the trick – Michel Tobon May 24 '14 at 20:17
  • @MichelTobon what do you mean by activities? I have a js interval running X amount of minutes depending on the user's choice in settings, however the time it lasts alive is random between 3-10 minutes after further testing. Maybe I'm making something wrong, can you share a jsfiddle, gist or similar link about the implementation? – Juan Carlos Alpizar Chinchilla May 27 '14 at 11:42
  • @JuanCarlosAlpizarChinchilla the best solution I found was to change the plugin for BackgroundMode: https://build.phonegap.com/plugins/490 it's easier and it maintains the background actions longer, good luck – Michel Tobon May 28 '14 at 19:25
  • @MichelTobon sorry for bugging but that one wasn't even working when I tested it, can you please add a quick fiddle to check it out? – Juan Carlos Alpizar Chinchilla May 29 '14 at 20:03
  • 1
    @JuanCarlosAlpizarChinchilla this is the fiddle, it's really simple http://jsfiddle.net/EJE4R/6/ – Michel Tobon May 31 '14 at 00:08
  • The problem with plugin 490 is that your app will probably be rejected on the App Store: https://github.com/katzer/cordova-plugin-background-mode/issues/77 With https://github.com/jocull/phonegap-backgroundjs (on which 430 is based) I notice the JS stops after around 3 minutes, so in ```onresume``` I try to check if things are still running or not. – user276648 Sep 15 '15 at 02:17
2

You can try to add plugin cordova-plugin-background-mode

But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed. Use the plugin by your own risk!

Maxim
  • 13,029
  • 6
  • 30
  • 45