简体   繁体   English

startForeground()导致cordova出现问题

[英]startForeground() causing problems with cordova

I'm using a service in my cordova app which generates a startForeground notification in the onCreate. 我在我的cordova app中使用了一个服务,它在onCreate中生成一个startForeground通知。 If the app is killed via swipe and started again, the cordova device-ready event isn't fired anymore. 如果应用程序通过滑动被杀死并再次启动,则不会再触发cordova设备就绪事件。 It seems the cordova object itsself is not defined when the app is restarted. 当应用程序重新启动时,似乎没有定义cordova对象本身。 I get the following error message: 我收到以下错误消息:

E/Web Console(19472): Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/src/jsFile.js:31

Line 31 and following: 第31行及以下:

cordova.exec(function (r) {
            if (callback) {
                callback(r);
            }
        }, function (err) {
            if (callback) {
                callback(false);
            }
        }, "Plugin", "functionName", []); 

The problem only occurs if I use the startForeground in my service, if I comment this part of the code out it's all working properly. 只有在我的服务中使用startForeground时才会出现问题,如果我对代码的这一部分进行评论,那么它的工作正常。 I desperately need some help on these questions: 我迫切需要一些关于这些问题的帮助:

  • Is it possible to get cordova running manually? 是否可以手动运行cordova?
  • How to start the service with startForeground , but still trigger the cordova device-ready after killing and restarting the app 如何使用startForeground启动服务,但在杀死并重新启动应用程序后仍然会触发cordova设备就绪

Edit: 编辑:

After making two quick example projects, one in Cordova 3.6 (the version I'm using for my actual App) and on with Cordova 5.1, I realized that the problem does not occur in the new Cordova version. 在做了两个快速示例项目之后,一个在Cordova 3.6(我用于我的实际应用程序的版本)和Cordova 5.1上,我意识到新Cordova版本中不会出现问题。 However I don't really want to update, because: 但是我真的不想更新,因为:

  • Never touch a running system 切勿触摸正在运行的系统
  • I don't know what might not work anymore in the never version, as I have installed many plugins in my App. 我不知道在Never版本中可能不起作用的东西,因为我在我的App中安装了很多插件。
  • I'm lazy. 我很懒。

Any idea what could have changed from 3.6 to 5.1 that solved the bug, and whether I could update my 3.6 code accordingly? 知道什么可以从3.6改为5.1解决了这个bug,以及我是否可以相应地更新我的3.6代码?

Edit2: EDIT2:

This is not a duplicate of this question . 不是 这个问题的重复。 The deviceReady callback was passed wrongly in this question. 在这个问题中错误地传递了deviceReady回调。 This is not the case in my app, and it wouldn't make sense to only work when I don't use startForeground in my service. 在我的应用程序中不是这种情况,只有在我的服务中不使用startForeground时才工作是没有意义的。

It seems your having an issue surrounding your apps life cycle. 您的应用生命周期似乎存在问题。 When you swipe of or forcefully close the app, the apps process is killed so when restarting if you had not saved dependent objects or variables in the state bundle they will come back as null because the activity is assuming those things are available. 当您滑动或强行关闭应用程序时,应用程序进程将被终止,因此如果您未在状态包中保存依赖对象或变量,则重新启动时它们将返回null,因为活动假定这些内容可用。 Can you paste the code you have in the onCreate() am guessing thats where your defining cordova. 你可以将你在onCreate()的代码粘贴到你定义的cordova那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM