如何在 ionic/cordova/phonegap 中检查在前台或后台运行的应用程序 [英] how to check app running in foreground or background in ionic/cordova/phonegap

查看:25
本文介绍了如何在 ionic/cordova/phonegap 中检查在前台或后台运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在ionic/cordova/phonegap中检查应用程序是在前台还是后台运行的,我需要在android和ios上使用,非常感谢

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

推荐答案

使用两个事件Pause";和简历".您可以在 Apache Cordova 事件文档 中找到所有事件一>.

Use the two Events "Pause" and "Resume". You will find all Events here in the Apache Cordova Events Documentation.

事件 - 暂停:

  • 当本机平台将应用程序置于后台时,通常会在用户切换到其他应用程序时触发暂停事件.

活动 - 恢复

  • 本机平台拉取应用程序时会触发 resume 事件从后台出来.

你可以在你的代码中添加一个事件监听器.对于这两个事件:

You can add an Eventlistener for that into your code. For those two Events that would be:

暂停 - 快速示例

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>

尝试一下,如果您需要进一步的帮助,请告诉我!

Try that out and let me know, if you need further help!

这篇关于如何在 ionic/cordova/phonegap 中检查在前台或后台运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆