使用Phonegap创建Android服务? (有phonegap应用程序运行,即使关闭) [英] Creating an Android Service with Phonegap? (Have phonegap app run even when closed)

查看:134
本文介绍了使用Phonegap创建Android服务? (有phonegap应用程序运行,即使关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Phonegap的Android应用程序,现在想使它所以当应用程序关闭它仍然可以执行java / js代码在应用程序。所以我明白我需要创建一个服务。如果我在phonegap上创建一个服务插件,我仍然可以执行javascript代码或只有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.

在我开发之前,如果我不能想到我会问,如果有人做过这个。我似乎找不到任何类似的电话gapg插件。

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代码作为服务。但是当它调用sendjavascript它不工作。

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代码(至少在我看来)作为服务。 Android上的Phonegap使用一种名为 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.

从Google网上论坛链接的代码尝试绑定以Java开发的服务

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.

您可以在从DroidGap活动派生的子活动中的JS代码中有一些后台活动。例如,在你的活动中有一个后台线程,有一个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.

更新:

JS代码只能使用Droidgap活动执行。活动可以有3种状态(基于活动生命周期):


  1. 可见

  2. 不可见但尚未加载

  3. li>
  1. visible
  2. invisible but still loaded
  3. not loaded

我提供了示例我实现了一个Phonegap插件。该插件允许活动将自己注册到SMS_RECEIVED。当活动超出范围(事件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.

这篇关于使用Phonegap创建Android服务? (有phonegap应用程序运行,即使关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆