Creating an Android Service with Phonegap? (Have phonegap app run even when closed)

别说谁变了你拦得住时间么 提交于 2019-11-26 11:10:31

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.

Bruno

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.

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!