如何检查应用程序运行在前景或背景在离子/ cordova / phonegap [英] how to check app running in foreground or background in ionic/cordova/phonegap
问题描述
有什么方法来检查应用程序是否在前台或后台运行在离子/ cordova / phonegap,我需要使用它在Android和ios,非常感谢
使用两个事件暂停
和恢复
。您可以在 Apache Cordova活动文档中找到所有活动。
事件 - 暂停:
事件 - 恢复
- 当本机平台从背景中移除应用程式
时,resume事件会启动。 b $ b
您可以在代码中添加一个Eventlistener。对于以下两个事件:
暂停 - 快速示例
document.addEventListener(pause,onPause,false);
function onPause(){
//处理暂停事件
}
或完整示例:
<!DOCTYPE html&
< html>
< head>
< title>暂停示例< / title>
< script type =text / javascriptcharset =utf-8src =cordova.js>< / script&
< script type =text / javascriptcharset =utf-8>
//等待设备API库加载
//
函数onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}
//设备API可用
//
function onDeviceReady(){
document.addEventListener(pause,onPause,false) ;
}
//处理暂停事件
//
function onPause(){
}
<脚本>
< / head>
< body onload =onLoad()>
< / body>
< / html>
恢复 - 快速示例
document.addEventListener(resume,onResume,false);
function onResume(){
//处理恢复事件
}
或完整示例如下
:
<!DOCTYPE html&
< html>
< head>
< title>恢复示例< / title>
< script type =text / javascriptcharset =utf-8src =cordova.js>< / script&
< script type =text / javascriptcharset =utf-8>
//等待设备API库加载
//
函数onLoad(){
document.addEventListener(deviceready,onDeviceReady,false);
}
//设备API可用
//
function onDeviceReady(){
document.addEventListener(resume,onResume,false) ;
}
//处理恢复事件
//
函数onResume(){
}
<脚本>
< / head>
< body onload =onLoad()>
< / body>
< / html>
如果您需要更多帮助,请尝试一下,让我知道!
Is there any way to check whether the app is running in foreground or background in ionic/cordova/phonegap, I need to use it on android and ios, thanks a lot
解决方案Use the two Events "
Pause
" and "Resume
". You will find all Events here in the Apache Cordova Events Documentation.Event - Pause:
- The pause event fires when the native platform puts the application into the background, typically when the user switches to a different application.
Event - Resume
- The resume event fires when the native platform pulls the application out from the background.
You can add an Eventlistener for that into your code. For those two Events that would be:
Pause - Quick Example
document.addEventListener("pause", onPause, false);
function onPause() {
// Handle the pause event
}
Or Full Example like this:
<!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>
Resume - Quick Example
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event
}
Or Full Example like this
<!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>
Try that out and let me know, if you need further help!
这篇关于如何检查应用程序运行在前景或背景在离子/ cordova / phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!