Creating an Android service using Phonegap? (You have a phone call application, even if it is closed)

I am working on an Android application using Phonegap, and now I would like to do it when the application is closed, it can still execute java / js code in the application. Therefore, I understand that I need to create a service. If I create a service plugin on the phone, can I execute javascript code or just java?

Does anyone do something like this? I found this discussion, but didn't seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6 So that's all I have right now.

Before moving on to developing his own, if I can’t understand, I thought that I would ask if anyone had done this before. It seems I can not find any of the plugins that will do something like this.

EDIT: I have an application that runs Java code as a service. However, when it calls sendjavascript, it does not work. So, is there a way for javascript code to run in the background when the application is closed using a phone call?

thank

+67
android android-intent mobile cordova android-service
Apr 27 2018-12-12T00:
source share
4 answers

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.

+54
May 03 '12 at 8:47 a.m.
source share

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

It uses a large plugin to easily create a background service with a telephone connection. But you cannot use JS though

I have not found a way to get JS to run in the background. BUT you can pass parameters from Java to JS and vice versa using a plugin ... which is very useful. You still have to rewrite your JS code in Java. Unless you have a specific reason for only JS to start? (But it should not be ...)

Hope this can be helpful for some people visiting this page.

+7
Jul 16 '13 at 20:18
source share

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

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

It allows you to run javascript in the background and in combination with a local notification plugin, you can even send notifications to the user at any time, just keep in mind that this will drain the battery faster, also consider that this can create an issue with the iOS policy . good luck !!!

+4
Feb 10 '14 at 13:44
source share

You can try adding the plugin cordova-plugin-background-mode

But as the author says:
Endless background tasks are not officially supported by most mobile operating systems and therefore do not correspond to public stores. Successful submission is not guaranteed. Use the plug-in at your own risk!

+2
Feb 13 '17 at 15:56
source share



All Articles