有没有办法检查应用程序是否在ionic / cordova / phonegap的前台或后台运行,我需要在android和ios上使用它,非常感谢
使用两个事件“Pause
”和“Resume
”。您将在Apache Cordova Events Documentation找到所有活动。
事件 - 暂停:
活动 - 简历
您可以在其代码中添加Eventlistener。这两个事件将是:
暂停 - 快速示例
document.addEventListener("pause", onPause, false);
function onPause() {
// Handle the pause event
}
或像这样的完整示例:
<!DOCTYPE html>
<html>
<head>
<title>Pause Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
document.addEventListener("pause", onPause, false);
}
// Handle the pause event
//
function onPause() {
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
简历 - 快速示例
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event
}
或像这样的完整示例
<!DOCTYPE html>
<html>
<head>
<title>Resume Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
document.addEventListener("resume", onResume, false);
}
// Handle the resume event
//
function onResume() {
}
</script>
</head>
<body onload="onLoad()">
</body>
</html>
如果您需要进一步的帮助,请试试并告诉我们!
对于Ionic 2和Ionic 3,解决方案是:
import { Platform } from 'ionic-angular';
@Component({
template: `OK`
})
constructor(public platform: Platform) {
platform.ready().then(() => {
if (platform.is('cordova')){
//Subscribe on pause
this.platform.pause.subscribe(() => {
//Hello pause
});
//Subscribe on resume
this.platform.resume.subscribe(() => {
window['paused'] = 0;
});
}
});
}
基于Sithys的Ionic小型服务回答:
factory('BackgroundCheck', function($ionicPlatform){
var service = {};
var inBackground = false;
$ionicPlatform.ready(function() {
document.addEventListener("resume", function(){inBackground = false;}, false);
document.addEventListener("pause", function(){inBackground = true;}, false);
});
service.isActive = function(){
return inBackground == false;
}
return service;
})
是。
1)当应用程序变为非活动状态(在后台运行)时,Cordova会触发pause
事件,当应用程序再次变为活动状态(带到前台)时,Cordova会触发resume
事件。
2)从这些事件中,可以使用变量将状态存储为“前景”或“背景”。
document.addEventListener("deviceReady", function readyCallback() {
var isAppInForeground = true;
document.addEventListener("pause", function pauseCallback() {
isAppInForeground = false;
}, false);
document.addEventListener("resume", function resumeCallback() {
isAppInForeground = true;
}, false);
});
使用ionic.Platform
的角度抽象
//The pause event fires when the native platform puts the application
// into the background, typically when the user switches to a different
// application.
$ionicPlatform.on('pause', function () {
// Handle event on pause
});
// The resume event fires when the native platform
// pulls the application out from the background.
$ionicPlatform.on('resume', function () {
// Handle event on resume
});
您还可以使用:
import { Platform, Config, MenuController } from '@ionic/angular';
...
constructor( private platform: Platform)
...
this.platform.resume.subscribe(() => {
// Handle event on resume
});
this.platform.pause.subscribe(() => {
// Handle event on pause
});