使用 Phonegap 创建 Android 服务?(即使关闭也让 phonegap 应用程序运行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10343828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an Android Service with Phonegap? (Have phonegap app run even when closed)
提问by Jonovono
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
我一直在使用 Phonegap 开发一个 Android 应用程序,现在想要让它在应用程序关闭时仍然可以执行应用程序中的 java/js 代码。所以我明白我需要创建一个服务。如果我在 phonegap 上创建了一个服务插件,我还可以执行 javascript 代码还是只执行 java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6So that is all I have right now.
有没有人做这样的事情?我发现这个讨论但似乎没有工作:http: //groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6所以这就是我现在所拥有的。
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
在我转向本地开发之前,如果我无法弄清楚,我想我会问是否有人以前做过这件事。我似乎找不到任何做类似事情的 phonegap 插件。
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
编辑:我有一个将 Java 代码作为服务执行的应用程序。但是,当它调用 sendjavascript 时它不起作用。那么有没有办法让 javascript 代码在使用 phonegap 关闭应用程序时也在后台运行?
Thanks
谢谢
采纳答案by ChrLipp
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.
不,不可能在后台运行 Javascript 代码(至少在我看来)作为服务。Android 上的 Phonegap 使用一个名为Droidgap的特殊活动,它托管一个 WebView。此浏览器控件执行 JavaScript。这意味着 JS 执行只能在这个 Activity 内部处理,不管它是否可见。
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.
您从 Google Groups 链接的代码试图将用 Java 开发的服务绑定到 DroidGap 活动,因此该服务不是用 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.
您可以在从 DroidGap 活动派生的子活动中的 JS 代码中设置一些后台活动。例如,在您的活动中有一个后台线程,有一个 JS 回调函数并让线程调用此回调功能。
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):
更新:
JS 代码只能通过 Droidgap 活动执行。一个活动可以有 3 个状态(基于活动的生命周期):
- visible
- invisible but still loaded
- not loaded
- 可见的
- 不可见但仍加载
- 未加载
I provided a samplein 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.
我提供了一个示例,其中我实现了一个 Phonegap 插件。该插件允许活动将自己注册到 SMS_RECEIVED。当活动超出范围(事件 onbeforeunload)时,它会取消注册,因此只处理问题 1。
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.
当您希望处理所有 3 个问题时,您必须将传入的 SMS 意图转发到活动。当它未加载时,系统将自动加载并激活该活动。但这不再是后台服务,只要收到短信,您的应用就会变得可见。
If you don't want this (if you really want a background service), you have to provide a native implementation.
如果你不想要这个(如果你真的想要一个后台服务),你必须提供一个本地实现。
回答by Bruno
There is this articleon how to create a service on Android with Phonegap which gives some good information on your problem.
有一篇关于如何使用 Phonegap 在 Android 上创建服务的文章,它提供了有关您的问题的一些很好的信息。
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
它使用了一个很棒的插件来轻松构建带有 phonegap 的后台服务。但是你不能使用JS
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...)
我没有找到让 JS 在后台运行的方法。但是您可以使用插件将参数从 Java 传递到 JS,反之亦然……这非常有用。不过,您仍然需要用 Java 重写 JS 代码。除非您确实有特定的理由只想运行 JS?(但应该没有……)
Hope that could be useful to some people visiting this page.
希望这对访问此页面的某些人有用。
回答by Michel Tobon
YES, and it is very simple... just install the plugin backgroundJS:
是的,而且很简单……只需安装插件backgroundJS:
https://build.phonegap.com/plugins/430
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!!!
它允许您在后台运行 javascript 并结合本地通知插件,您甚至可以随时向用户发送通知,请记住这样做会导致电池更快耗尽,也考虑到这可能使用 iOS 策略创建问题。祝你好运!!!
回答by Maxim
You can try to add plugincordova-plugin-background-mode
你可以尝试添加插件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!
但是正如作者所说:
无限后台任务在大多数移动操作系统上都没有官方支持,因此不符合公共商店供应商的要求。不保证成功提交。使用插件风险自负!