有没有任何方法来检查这个应用程序是在前台运行还是在后台运行,在离子型/cordova/phonegap中,我需要在android和ios上使用它,非常感谢
发布于 2015-04-13 15:43:08
使用两个事件"Pause“和"Resume”。您将在Apache事件文档中找到所有事件。
活动-暂停:
活动-恢复
您可以为此在代码中添加一个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>试试看,如果你需要进一步的帮助,请告诉我!
发布于 2017-06-23 10:22:06
对于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 i.e. background
this.platform.pause.subscribe(() => {
//Hello pause
});
//Subscribe on resume i.e. foreground
this.platform.resume.subscribe(() => {
window['paused'] = 0;
});
}
});
}
发布于 2016-06-26 21:36:20
一项基于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;
})https://stackoverflow.com/questions/29606012
复制相似问题