How to keep Javascript code running in background on Android

4,560 views
Skip to first unread message

Vincent Lasmarias

unread,
Sep 5, 2011, 4:45:18 PM9/5/11
to phonegap
Android issue: I have some Javascript code running in a loop which I
want to keep running even after the Back button is pressed on the Home
screen. From the Android lifecycle docs, it sounds like this calls the
Activity.onDestroy() which essentially stops the Javascript code
because it's not registered as a Service? What's the easiest way for
me to keep the javascript running even after the Back button is
pressed? I want to avoid porting all the Javascript code (which works
great on the iPhone) to a native background Service. Thanks in advance
for the answers.

Devgeeks

unread,
Sep 5, 2011, 7:19:42 PM9/5/11
to phon...@googlegroups.com
Isn't the back button like a sort of "quit" while the home button is for putting an app in the background?

Forgive my Android-ignorance.

Simon MacDonald

unread,
Sep 5, 2011, 8:03:37 PM9/5/11
to phon...@googlegroups.com
No you are correct. Clicking the back button will quit the
application. Using the home button will put it in the background and
your background JS will continue to run.

Simon Mac Donald
http://hi.im/simonmacdonald

> --
> 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
>

Cody miranda

unread,
Sep 5, 2011, 8:10:01 PM9/5/11
to phon...@googlegroups.com

Useing the back button until an application or function disappears usually closes the whole thing. Try simply pushing the home button once usually leaves things running n. The background.

Steve Sobol

unread,
Sep 5, 2011, 7:26:10 PM9/5/11
to phon...@googlegroups.com
Devgeeks wrote:
Isn't the back button like a sort of "quit" while the home button is for putting an app in the background?

Correct on both counts.



--
Lobos Studios - Website Design, Programming, IT Support
Phone: 877.919.4WEB (877.919.4932) (Toll-free)
Phone: 760.684.8859 (Victor Valley)
www.LobosStudios.com -- www.facebook.com/LobosStudios
Twitter: @LobosStudios
 

Vincent Lasmarias

unread,
Sep 6, 2011, 1:36:30 AM9/6/11
to phonegap
From what I can find out in the Android docs, the Back button is
supposed to call onDestroy() for the current activity _but_ not kill
the app. In fact, pressing the Back button on our PhoneGap app still
keeps it in the TaskKiller list of still-running apps.

After doing some research, we narrowed down the cause for our
Javascript code getting killed to the following code in DroidGap's
onDestroy() method:

// Load blank page so that JavaScript onunload is called
this.appView.loadUrl("about:blank");

// Forward to plugins
this.pluginManager.onDestroy();

After commenting those out and replacing them with
'moveTaskToBack(true)', this allowed us to get the behaviour we wanted
- i.e., pressing the Back button on our app's starting screen moved
the screen back to the Activity prior to our app being started but
still kept our looping Javascript code running.

Any negative side-effects of doing what I did? From what I can see,
everything still seems to be working fine.

On Sep 5, 5:03 pm, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> No you are correct. Clicking the back button will quit the
> application. Using the home button will put it in the background and
> your background JS will continue to run.
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Dave Harris

unread,
Sep 29, 2011, 11:10:11 PM9/29/11
to phonegap
How is the user able to close your app? I have similar requirements
but don't want to have the app running until a force close or
shutdown.

On Sep 5, 11:36 pm, Vincent Lasmarias <vincent.lasmar...@gmail.com>
wrote:

Ori Harel

unread,
May 28, 2017, 3:14:07 AM5/28/17
to phonegap, vincent....@gmail.com
You can run Javascript inside a WebView from a background Service. you need to use WindowManager. Check out this post here.
Reply all
Reply to author
Forward
0 new messages