【问题标题】:Blackberry Web Works how to fire event everytime the app is launchedBlackberry Web Works如何在每次启动应用程序时触发事件
【发布时间】:2012-05-11 04:24:34
【问题描述】:
<html>
  <head>
    <meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no"/>
    <script type="text/javascript">
    function helloWorld() {
     alert("Hello World");
    }
    </script>
  </head>
  <body onload="helloWorld();">
   <h1>Hello World</h1>
  </body>
</html>

我有一个使用类似于上面的 Blackberry WebWorks 构建的应用程序。每次用户打开应用程序时,我都需要触发上述 helloWorld() 函数。

问题是“onload”功能仅在应用首次启动时触发,或者当用户通过点击“手机上的挂断按钮”退出应用时触发,而不是在点击“手机上的后退按钮”时触发。

有什么建议吗?

【问题讨论】:

    标签: javascript jquery html blackberry blackberry-webworks


    【解决方案1】:

    我认为您不仅有兴趣在每次启动应用程序时启动该功能,而且在从后台检索应用程序时(这意味着当您的应用程序未关闭但它在后台运行时)你没有与之交互)。

    我建议您使用 Cordova(以前的 Phonegap)并查看 "resume" event。使用那里给出的例子,我认为你需要这样的东西:

    <html>
    <head>
      <title>Cordova Resume Example</title>
    
      <script type="text/javascript" charset="utf-8" src="cordova-1.7.0.js"></script>
      <script type="text/javascript" charset="utf-8">
    
      // Call onDeviceReady when Cordova is loaded.
      // At this point, the document has loaded but cordova-1.7.0.js has not.
      // When Cordova is loaded and talking with the native device,
      // it will call the event `deviceready`.
      function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
      }
    
      // Cordova is loaded and it is now safe to make calls Cordova methods
      function onDeviceReady() {
          document.addEventListener("resume", onResume, false);
          // Call the function you are interested in.
          helloWorld();
      }
    
      // Handle the resume event
      function onResume() {
        helloWorld();
      }
    
      function helloWorld(){
        alert('Hello World');
      }
      </script>
    </head>
    <body onload="onLoad()">
    
    </body>
    </html>
    

    您可以下载您需要的文件here。我没有测试代码。试试看,让我知道它是否适合你。

    【讨论】:

    • 感谢您的回复,但我决定放弃 WebWorks,现在使用 Eclipse 和 Java 进行本地化 - 到目前为止一切都非常顺利,我所需要的只是覆盖“activate()”函数。无法尝试您的解决方案,但要投票。谢谢。
    • 本地化可能是最好的选择。祝你的应用好运;)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多