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

查看:34
本文介绍了使用 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.

在我转向本地开发之前,如果我无法弄清楚,我想我会问是否有人以前做过这件事.我似乎找不到任何做类似事情的 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 代码作为服务执行的应用程序.但是,当它调用 sendjavascript 时它不起作用.那么有没有办法在使用 phonegap 关闭应用程序时让 javascript 代码也在后台运行?

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 执行只能在这个 Activity 内部处理,不管它是否可见.

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 Groups 链接的代码试图将用 Java 开发的服务绑定到 DroidGap 活动,因此该服务不是用 JS 编写的.

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. 未加载

我提供了一个示例,其中我实现了一个 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天全站免登陆