创建一个Android服务与PhoneGap的? (甚至关闭时PhoneGap的应用程序运行)创建一个、应用程序、Android、PhoneGap

2023-09-12 00:13:45 作者:╰ァ笑顔如花ゞ

我一直在使用PhoneGap的Andr​​oid应用程序,现在想使它所以当应用程序被关闭它仍然可以在执行java / JS code的应用程序。所以,我明白,我需要创建一个服务。如果我创建的PhoneGap服务插件我还可以执行JavaScript code或仅在Java?

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?

有没有人做这样的事情?我发现这个讨论,但似乎并没有工作:http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6 所以,这就是我现在所拥有的。

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/722b0e796baa7fc6 So that is all I have right now.

在我转向开发它原生,如果我不出来想我会问,如果有人已经这样做过。我似乎无法找到任何的PhoneGap的插件,做同样的事情的。

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.

编辑:我有一个执行的Java code作为一种服务的应用程序。然而,当它调用sendjavascript这是行不通的。那么,有没有办法让在后台的JavaScript code运行以及当一个应用程序与PhoneGap的关闭?

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?

感谢

推荐答案

没有,那是不可能的在后台运行(至少在我看来)的Javascript code作为一种服务。 PhoneGap的Andr​​oid上使用一个特殊的活动名为Droidgap,它承载的WebView。该浏览器控件执行的JavaScript。这意味着,JS执行只能这个活动里面来处理,不论它是可见或不可见。

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.

您从谷歌论坛链接的code试图绑定Java开发的DroidGap活动服务,所以服务是不是用J​​S。

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.

您可以在您的JS code一些背景里面的活动从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.

如果你真的需要一个服务,你必须去当地人。

If you really need a service you have to go native.

更新1: JS code只能用Droidgap活动的执行。一个活动可以有3种状态(根据生命周期):

Update 1: JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):

可见 在无形的,但仍然加载 在没有加载

我公司提供的,其中我实现了一个PhoneGap的插件,一个样本。该插件允许该活动将其自身注册为SMS_RECEIVED。当activies超出范围(事件onbeforeunload的),将注销,所以唯一的问题1的处理。

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.

当你要处理的所有3个问题,你要转发接收到的SMS意图的活动。当没有装载系统会自动加载并激活活性。但是,这不是一个后台服务了,你的应用程序将变得可见每次收到短信。

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.

更新2: 有在 Github上,它通过显示一个面包和通知解决了闲置另一种实现。当用户选择所述通知转发到应用程序。

Update 2: There is another implementation on Github, which solves the inactivity by displaying a toast and a notification. When the user selects the notification it forwards to the application.