保持运行 PhoneGap/Cordova [英] keepRunning PhoneGap/Cordova

查看:37
本文介绍了保持运行 PhoneGap/Cordova的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释 keepRunning 在 Android 的 config.xml 中是如何工作的.

Anyone can explain me how keepRunning works in the config.xml for Android.

我的意思是,我不想知道如何编写指令,但它是如何工作的,它如何影响 Android 应用程序的执行?它是否在后台创建服务?

I mean, I don't want to know how to write the instruction but how does it work, how does it affect the execution of the Android app ? Does it create an Service in background ?

如果有人能找到我们可以看到它是如何工作的源代码,那就太好了

If anyone can find the source where we can see how does it work, that will be great

谢谢.

我尝试分析生成的代码,分析Android设置中的RAM、服务和进程.而我的结论是......什么都不做.如果您尝试制作使用 GPS 跟踪用户的应用程序,请不要使用 Cordova.要正确跟踪用户,您需要使用 START_STICKY 选项创建服务.所以,它是在本机代码中.你失去了对 CrossPlatform 的兴趣,因为你必须为所有平台重新编码服务,在我看来,Native Service 和 Cordova App 之间的通信并不容易.

Edit : I try to analyze the generated code, analyze the RAM, services and processus in the setting of Android. And my conclusion is..... that do nothing. If you try to make a app which track the user with GPS, dont use Cordova. To track the user correctly, you need to make a Service with the START_STICKY option. So, it's in native code. you lost the interest of the CrossPlatform because you have to recode the service for all platforms and in my opinion, the communication between Native Service and Cordova App is not easy.

总而言之,如果你使用Cordova,你必须知道你不能使用所有native的力量,你必须做出选择:- 简单的开发(主观)和跨平台(真的跨平台?)和- 原生开发具有强大的功能且没有兼容性问题,但您必须为一个平台制作一个应用

In conlusion, if you use Cordova, you have to know you can't use the power of all native, you have to make choise : - easy dev (subjective) and crossplaform (really crossplatform ?) and - Native dev with its power and no compatibility problems but you have to make one app for one platform

推荐答案

我不是 JS/Cordova 开发人员,我是 Android 开发人员.有一次我开发了 Cordova 插件,遇到了一些问题,并对此主题进行了一些调查.

I'm not a JS/Cordova developer, I'm an Android developer. Once I worked on a Cordova plugin, faced some issues and did some investigations on the subject.

keepRunning 标志的一般目的是指示在应用暂停(进入后台)时是否应停止 JS 计时器.回答您的问题:不,它不会创建任何新的服务.现有的设计在这方面很简单.

General purpose of keepRunning flag is to indicate if JS timers should be stopped when the app is paused (goes to background). Answering your question: no, it doesn't create any new Service. Existing design is quite plain in this regard.

keepRunning 标志在 CordovaActivity.java 如下:

// Keep app running when pause is received. (default = true)
// If true, then the JavaScript and native code continue to run in the background
// when another application (activity) is started.
protected boolean keepRunning = true;

其主要目的是在 CordovaWebView.java:

Its main purpose is to disable JS timers when Cordova app is paused, in CordovaWebView.java:

public void handlePause(boolean keepRunning)
{
    LOG.d(TAG, "Handle the pause");
    // Send pause event to JavaScript
    this.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};");

    // Forward to plugins
    if (this.pluginManager != null) {
        this.pluginManager.onPause(keepRunning);
    }

    // If app doesn't want to run in background
    if (!keepRunning) {
        // Pause JavaScript timers (including setInterval)
        this.pauseTimers();
    }
    paused = true;

}

请注意,插件也通过 PluginManager 收到通知,因此理论上它们可以处理应用暂停事件,以在后台停止(或不)其活动,具体取决于 keepRunning标志.

Note that plugins are also notified via PluginManager, so in theory they can handle app paused events, to stop (or not) their activity in background, depending on keepRunning flag.

就我而言,当 keepRunningtrue 时,我遇到了问题/错误,但 JS 计时器无论如何都停止了.这是因为在 CordovaActivity.java:

In my case I had an issue/bug when keepRunning was true, but JS timers were stopped anyway. It happened because there is additional functionality related to that flag, in CordovaActivity.java:

/**
 * Launch an activity for which you would like a result when it finished. When this activity exits,
 * your onActivityResult() method will be called.
 *
 * @param command           The command object
 * @param intent            The intent to start
 * @param requestCode       The request code that is passed to callback to identify the activity
 */
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
    this.activityResultCallback = command;
    this.activityResultKeepRunning = this.keepRunning;

    // If multitasking turned on, then disable it for activities that return results
    if (command != null) {
        this.keepRunning = false;
    }

    // Start activity
    super.startActivityForResult(intent, requestCode);
}

当 Cordova 应用启动另一个 Android 活动时,主要的 Cordova 活动(带有 WebView 的屏幕)进入后台并因此暂停.就我而言,它是通过 Google 地图插件制作的,该插件通过 Cordova 应用程序启动了 GM 屏幕.

When Cordova app launches another Android activity, main Cordova activity (screen with WebView) goes to background and is therefore paused. In my case it was made via Google Maps plugin which started GM screen over Cordova app.

上面的代码关闭了keepRunning标志,这意味着当被调用的活动出现时(在CordovaActivity.onPause方法中)JS计时器无论如何都会停止,无论keepRunning是真还是假!

The code above turns off keepRunning flag, and it means that JS timers are stopped anyway when the called activity appears (in CordovaActivity.onPause method) regardless keepRunning is true or false!

它看起来像是一种为某些不清楚(且未记录)的目的而实施的技巧,我不知道它的上下文.就我而言,它导致了一个错误,我只是删除了 startActivityForResult 中的 keepRunning 处理,重新编译了 Cordova,它运行正常.

It looks like a kind of trick implemented for some unclear (and not documented) purpose, I do not know its context. In my case it caused a bug, and I just removed keepRunning handling in startActivityForResult, recompiled Cordova and it worked OK.

添加:关于使用 GPS 服务 - 你说得对,我同意.作为具有相关 (GPS) 经验的 Android 开发人员,我可以说正确的方法(可能也是唯一可接受的)是为此使用服务.据我所知,Cordova 没有为它提供任何功能,所以我认为它应该通过插件来制作.我的意思是您可以为 GPS 功能(作为服务实现)编写原生 Android 代码并从 JS 代码访问它.我相信这是 Cordova 中针对此类情况的常见解决方案.

ADDED: About using a Service for GPS - you are quite right, I agree. As an Android developer with relevant (GPS) experience I can say that a right approach (and possible the only acceptable) is to use a service for that. As far as I know Cordova doesn't provide any functionality for it, so I think it should be made via a plugin. I mean you can write native Android code for GPS functionality (implemented as a Service) and access it from JS code. I believe it is a common solution in Cordova for such cases.

这篇关于保持运行 PhoneGap/Cordova的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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