keepRunning的PhoneGap /科尔多瓦 [英] keepRunning PhoneGap/Cordova

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

问题描述

任何人都可以keepRunning如何工作在config.xml为Android解释我。

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

感谢。

编辑:我尝试分析生成的code,分析RAM,服务和突Android中的设置。而我的结论是.....什么也不做。 如果你试图让一个应用程序,它跟踪了GPS的用户,不使用科尔多瓦。为了正确地跟踪用户,你需要用START_STICKY选项进行服务。因此,它在本土code。你失去了跨平台的兴趣,因为你必须重新code在所有平台上,在我看来的服务,本地服务和科尔多瓦应用程序之间的通信是不容易的。

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.

在conlusion,如果你使用的科尔多瓦,你必须知道你不能使用的所有本机的电源,你必须做出的choise: - 简单的开发(主观)和crossplaform(真正的跨平台?) 和 - 原生开发,其权力和不存在兼容性问题,但你必须做出一个应用程序的一个平台。

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 /科尔多瓦开发者,我是一个Android开发者。有一次,我参与了科尔多瓦的插件,遇到了一些问题,并做了一些调查,关于这个问题的。

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.

一般目的是表明,如果 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 标记在<定义href="https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaActivity.java"相对=nofollow> 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;

它的主要目的是禁用的 JS计时器时,科尔多瓦的应用程序是暂停,在<一个href="https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaWebView.java"相对=nofollow> 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;

}

请注意,插件还通过通知插件管理,所以从理论上讲,他们可以处理的应用程序暂停活动,停止(或没有),他们在后台活动,根据 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.

在我来说,我有一个问题/错误时, keepRunning 中的真正的,但JS定时器进行反正停了下来。这事发生,因为没有涉及到该标志的附加功能,在<一个href="https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaActivity.java"相对=nofollow> 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);
}

在科尔多瓦应用程序启动另外一个Android的活动,主要科尔多瓦活动(屏幕的WebView )被切换到后台,因此被暂停。在我的情况下,它是通过谷歌地图插件,通用汽车开始在屏幕科尔多瓦程序进行。

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.

在code以上关闭的 keepRunning 标志,它意味着当一个activity出现(在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!

它看起来像一个把戏实施了一段不明确(而不是文件)的目的,我不知道它的背景。在我的情况下,它造成的一个bug,而我只是删除 keepRunning 处理中的 startActivityForResult ,重新编译科尔多瓦和它的工作确定。

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 - 你说的很对,我同意。作为一名Android开发者与相关(GPS)的经验,我可以说,正确的做法(以及可能唯一可以接受的),是使用服务的。据我所知科尔多瓦不提供任何功能,所以我觉得应该通过插件来进行。我的意思是,你可以编写原生Android code的GPS功能(实现为服务)和JS code访问它。我相信这是科尔多瓦这种情况下,一个通用的解决方案。

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.

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

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