keepRunning PhoneGap / Cordova [英] keepRunning PhoneGap/Cordova

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

问题描述

任何人都可以解释我如何在Android的config.xml中使用keepRunning。



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



如果任何人都可以找到源代码,我们可以看到它是如何工作的,这将是很好的



感谢。



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



conlusion,如果你使用Cordova,你必须知道你不能使用所有原生的力量,你必须做choise:
- 容易dev(主观)和crossplaform(真正crossplatform?)

- Native dev其强大功能和兼容性问题,但是你必须为一个平台创建一个应用程序

解决方案

不是JS / Cordova开发人员,我是Android开发人员。一旦我使用Cordova插件,就会遇到一些问题,并对这个问题进行了一些调查。



keepRunning 标志的一般用途是 JS 计时器应在应用程序暂停(转到后台)时停止。回答您的问题:不,它不会创建任何新的服务



keepRunning 标志在 CordovaActivity.java ,如下所示:

  //在收到暂停时保持应用程序运行。 (default = true)
//如果为true,那么当另一个应用程序(活动)启动时,JavaScript和本地代码继续在后台运行
//。
protected boolean keepRunning = true;

其主要目的是在Cordova应用程式时禁用 JS计时器 > paused ,位于 CordovaWebView.java

  public void handlePause(boolean keepRunning)
{
LOG.d 处理暂停);
//发送暂停事件到JavaScript
this.loadUrl(javascript:try {cordova.fireDocumentEvent('pause');} catch(e){console.log ');};);

//转发到插件
if(this.pluginManager!= null){
this.pluginManager.onPause(keepRunning);
}

//如果应用程序不想在后台运行
if(!keepRunning){
//暂停JavaScript计时器(包括setInterval)
this.pauseTimers();
}
paused = true;

}

注意,插件也通过 PluginManager ,因此根据 keepRunning 标志,理论上它们可以处理应用程序暂停的事件,以在后台停止(或不停止)其活动。



在我的情况下,当 keepRunning true 时,我遇到了一个问题/错误,但JS计时器已停止。这是因为在 CordovaActivity.java

  / ** 
*启动您想要的活动结果完成后。当此活动退出时,
*将调用onActivityResult()方法。
*
* @param命令命令对象
* @param intent启动的意图
* @param requestCode传递到回调以标识活动的请求代码
* /
public void startActivityForResult(CordovaPlugin命令,Intent intent,int requestCode){
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;

//如果多任务处于打开状态,则对于返回结果的活动禁用它
if(command!= null){
this.keepRunning = false;
}

//开始活动
super.startActivityForResult(intent,requestCode);当Cordova应用启动另一个Android活动时,主科尔多瓦活动(屏幕上显示 > WebView )转到背景,因此暂停。



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



它看起来像一种实现的技巧一些不清楚(而且没有记录)的目的,我不知道它的上下文。在我的情况下,它引起了一个错误,我只是在 startActivityForResult 中删除 keepRunning ,重新编译Cordova,它工作正常。



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


Anyone can explain me how keepRunning works in the config.xml for 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

Thanks.

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.

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

解决方案

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.

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.

The keepRunning flag is defined in CordovaActivity.java as follows:

// 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;

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;

}

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.

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);
}

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.

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!

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.

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 / Cordova的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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