No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses a special kind of Droidgap activity that hosts WebView. This browser control runs JavaScript. This means that JS execution can only be processed inside this action, regardless of whether it is visible or not.
The code associated with Google groups is trying to associate a Java-developed service with DroidGap activity, so the service is NOT written in JS.
In your JS code inside your child activity, you can get some background activity related to DroidGap activity. For example, you have a background thread in your activity, there is a JS callback function, and let the thread call this callback function.
If you really need a service, you need to go to your native language.
Update:
JS code can only be executed using the Droidgap operation. An activity can have 3 states (based on the life cycle of actions ):
- visible
- invisible but still loaded
- not loaded
I presented a sample in which I implemented the Phonegap plugin. The plugin allows activity to be registered on SMS_RECEIVED. When activity goes out of scope (onbeforeunload event), it deregisters, so only problem 1 is processed.
If you want all 3 problems to be handled, you need to redirect incoming SMS messages to activity. When it is not loaded, the system will automatically download and activate the action. But this is no longer a background service, your application will become visible when receiving SMS.
If you do not want this (if you really want to use the background service), you must provide your own implementation.
ChrLipp May 03 '12 at 8:47 a.m. 2012-05-03 08:47
source share