Running JavaScript from Android Background Service

1,367 views
Skip to first unread message

Mark Taylor

unread,
Jul 11, 2013, 7:58:09 AM7/11/13
to phon...@googlegroups.com
Before I get pointed at my own plugin ;)   -> I maintain the Android Background Service plugin which provides a framework for developers to run code in an Android Background Service.

One of the common questions I receive is "How do I run my javascript in the Background Service"?

My stock answer to date has been "you don't - rewrite your code into java and run it from my plugin".  This works (or has so far), but isn't ideal for a lot of people.

So I'm looking for ideas, pointers, etc on how I can provide a plugin which accepts javascript (as a string, file, whatever) pass that off to my Background Service and then have it execute within the Background Service context.

Unfortunately, other than for the plugin, I don't actively use Phonegap so am fairly ignorant of the internal workings.  My hope is that there is a JavaScript engine that I can just lift and re-purpose.

I'm unlikely to start cutting code for this before September (maintaining the existing plugin eats into my spare time quite a bit already) - so any advice prior to this would be great (hoping for the "standing on the shoulders of giants" technique).

Thanks.


Mark Taylor

unread,
Aug 14, 2013, 4:22:23 PM8/14/13
to phon...@googlegroups.com
Does anyone have any ideas on how to do?

Or does anyone have advice on who might be someone to approach for a deeper understanding of the inner workings of Cordova?

Ruchi Singla

unread,
Sep 2, 2013, 3:54:31 AM9/2/13
to phon...@googlegroups.com
Hello Mark,

Did u get the solution for this?i am trying to do the same.do share

Thanks

Mark Taylor

unread,
Sep 20, 2013, 4:57:34 PM9/20/13
to phon...@googlegroups.com
No, I'm afraid I haven't (sorry for the delay in response)

Chris Scott

unread,
Jan 10, 2014, 12:24:01 PM1/10/14
to phon...@googlegroups.com
+1

kere...@gmail.com

unread,
Mar 20, 2014, 1:49:08 PM3/20/14
to phon...@googlegroups.com
+1

Francys Reymer

unread,
Mar 20, 2014, 7:21:30 PM3/20/14
to phonegap

Chris Scott

unread,
Mar 20, 2014, 7:52:46 PM3/20/14
to phon...@googlegroups.com
See Mark's first sentence in this thread.

"Before I get pointed at my own plugin ;)"
On Thu, Mar 20, 2014 at 7:21 PM, Francys Reymer <francys...@gmail.com> wrote:

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
 
To compile in the cloud, check out build.phonegap.com
---
You received this message because you are subscribed to a topic in the Google Groups "phonegap" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phonegap/0cnXBtpaRGY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Chris Scott

Adam Laskowitz

unread,
Jul 30, 2014, 12:28:53 PM7/30/14
to phon...@googlegroups.com
Hi Mark,

I have just started using your plugin, went through the tutorials, and started implementing my own usage of the sample app.

Regarding your question, I want to let you know that the javascript code is sometimes run from the
updateHandler()
(named in the tutorial) or
handleSuccess()
(in the sample app) function that gets the result from the MyService.java file.

I am using a TTS library (http://plugreg.com/plugin/poiuytrez/SpeechRecognizer) and built a function called
speak(textToSpeak, callbackFunction)
I have a
timer()
function that is called every 1 second by your plugin (via
updateHandler()
and
enableTimer())

that adds 1 to a counter.  Inside the timer() function if I say
speak(counter, false)
then run the app, it will say out loud "one", "two", "three", etc.  This works with the phone screen off or the app sent to the background.  Although if I do something like 
if (counter % 5 == 0) {
    speak
(counter, false);
}
then this no longer works.  It will however work if I try 2 instead of 5.  Even though the function is called inside updateHandler every 1 second.

It is worth mentioning, this will always work if the phone is plugged into the computer and the phone screen is off, or the app is in the background.  So to test this I need to build/run and then unplug my phone.

I suppose this is part question (why does this behavior exist?) and part answer (sometimes you can actually run javascript using the BackgroundService plugin you wrote).

- Adam

Mark Taylor

unread,
Jul 31, 2014, 10:43:48 AM7/31/14
to phon...@googlegroups.com
Adam

I'd assume that it is working because the Android O/S hasn't terminated the app.  I believe the O/S will close apps as they leave the foreground based on a number of things - including memory usage.  Thus I'd assume if nothing is competing for the memory then it will leave the app running in the background.  This would explain the behavior you are seeing.

I certainly wouldn't rely on the app being available in the background - its more luck than design.

Someone else on the forum is likely to be better placed to confirm my assumption or laugh at my stupidity.

Mark


Adam Laskowitz

unread,
Aug 1, 2014, 4:13:38 PM8/1/14
to phon...@googlegroups.com
Mark, thank you so much for the reply!

Your answer makes sense to me (not being a very knowledgeable Android guy).  Although, it's strange that when it works it works consistently, day after day, even when I reboot the phone.  Once I change the code to call the function every 10 seconds for example, then it no longer works, consistently.

It feels like there's something else going on here.

Mark Taylor

unread,
Aug 4, 2014, 8:45:30 AM8/4/14
to phon...@googlegroups.com
Well that is really confusing me.  No way should it survive a reboot

The Cordova app really should be there for the plugin to call back to.

Love to understand what this is if you get to the bottom of it.

Ori Harel

unread,
May 28, 2017, 3:16:17 AM5/28/17
to phonegap
You can run Javascript in a background Service using a WebView and WindowManager. check out this post here.

jcesarmobile

unread,
May 29, 2017, 3:23:05 AM5/29/17
to phonegap
There is no need of posting your article on any old thread where somebody asked about background services
Reply all
Reply to author
Forward
0 new messages