gpt4 book ai didi

android - keepRunning PhoneGap/ Cordova

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:29 26 4
gpt4 key购买 nike

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

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

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

谢谢。

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

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

最佳答案

我不是 JS/Cordova 开发人员,我是 Android 开发人员。有一次我在 Cordova 插件上工作,遇到了一些问题,并对这个主题做了一些调查。

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

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;

它的主要目的是在 Cordova 应用暂停时禁用JS 计时器,在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 收到通知,因此理论上它们可以处理应用程序暂停事件,以停止(或不停止)它们在后台的 Activity ,具体取决于 keepRunning旗帜。

在我的例子中,当 keepRunningtrue 时,我遇到了问题/错误,但 JS 计时器无论如何都停止了。这是因为在 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 Activity 时,主要的 Cordova Activity (带有 WebView 的屏幕)进入后台并因此暂停。在我的例子中,它是通过谷歌地图插件制作的,该插件在 Cordova 应用程序上启动 GM 屏幕。

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

它看起来像是为了某些不明确(且未记录)的目的而实现的一种技巧,我不知道它的上下文。在我的例子中,它导致了一个错误,我只是删除了 startActivityForResult 中的 keepRunning 处理,重新编译了 Cordova,它工作正常。

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

关于android - keepRunning PhoneGap/ Cordova ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21001439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com