退出cordova Windows 10应用程序
问题描述:
当我在主页中使用我的cordova应用程序时,我需要向用户显示消息,询问他是否真的要退出应用程序。
如果用户选择“是”,我需要退出应用程序。退出cordova Windows 10应用程序
要做到这一点,我用这个代码:
document.addEventListener('backbutton', function() {
$rootScope.back();
$rootScope.$apply();
}, false);
$rootScope.back = function(execute) {
var path = $location.$$path;
if (path !== '/') {
$location.path(path.split('/').slice(0, -1).join('/'));
} else {
$rootScope.exitApp();
}
}.bind(this);
$rootScope.exitApp = function() {
$rootScope.$emit('showPopover', {
message: $rootScope.LABELS.QUIT_APP,
confirmCallback: function() {
if (typeof navigator.app !== 'undefined') {
navigator.app.exitApp();
}
},
cancelCallback: doNothing
});
};
它的工作在Android和iOS,但不是在Windows 10应用程序。
在W10中,navigator.app
未定义。
我读了我应该暂停的应用程序,而不是退出,所以我想这窗户科尔多瓦DOC quirkswritten(https://cordova.apache.org/docs/en/latest/cordova/events/events.html#backbutton):
$rootScope.exitApp = function() {
$rootScope.$emit('showPopover', {
message: $rootScope.LABELS.QUIT_APP,
confirmCallback: function() {
if (typeof navigator.app !== 'undefined') {
navigator.app.exitApp();
}
else if (platformUtils.isWindows()) {
throw new Error('Exit'); // This will suspend the app
}
},
cancelCallback: doNothing
});
};
throw new Error('Exit')
被调用,并在日志中显示错误,但应用不暂停。
也许是因为我在一个角度的应用程序?
有没有人有想法?
答
问题归因于$emit
用于显示弹出窗口。
如果我在popover回调上抛出错误,则此错误不会返回到backbutton
事件回调。
但是,在Windows平台上,我们不应该显示弹出窗口来询问用户是否一定要离开应用程序,所以如果我将它添加到“返回”功能,它就可以工作。使用到Windows
最终代码10的应用程序是:
document.addEventListener('backbutton', function() {
$rootScope.back();
$rootScope.$apply();
}, false);
$rootScope.back = function(execute) {
var path = $location.$$path;
if (path !== '/') {
$location.path(path.split('/').slice(0, -1).join('/'));
} else {
if (window.device.platform.toLowerCase() === 'windows') {
throw new Error('Exit'); // This will suspend the app
} else {
$rootScope.exitApp();
}
}
}.bind(this);
答
你也可以使用此:
if (device.platform.toLowerCase() === "windows") {
//this closes the app and leaves a message in the windows system eventlog if error object is provided
MSApp.terminateApp();
} else {
navigator.app.exitApp()
}