java - 使用 JSNI 在 GWT 中使用原生 Phonegape js

标签 java javascript gwt phonegap-plugins jsni

我想使用 JSNI 在 GWT 上运行此phonegap native 警报

navigator.notification.alert(
        'You are the winner!',  // message
         null,         // callback
        'Game Over',
        'Done'
  );

我已经尝试过这个:

public static native void testNativeAlert()/*-{

   navigator.notification.alert(
    'You are the winner!',  // message
     null,         // callback
    'Game Over',
    'Done');}-*/;

最佳答案

来自http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#writing ,不要忘记 $wnd 来访问全局属性!

public static native void alert(String msg) /*-{
  $wnd.alert(msg);
}-*/;

Note that the code did not reference the JavaScript window object directly inside the method. When accessing the browser’s window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page’s window and document.

因此,在您的情况下,您必须使用 $wnd 访问 navigator:

public static native void testNativeAlert()/*-{

   $wnd.navigator.notification.alert(
    'You are the winner!',  // message
    null,         // callback
    'Game Over',
    'Done');
}-*/;
<小时/>

旁注:文档不清楚,但您确定phonegap让callback为空吗?否则请进一步查看我上面链接的 JSNI 文档。

关于java - 使用 JSNI 在 GWT 中使用原生 Phonegape js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032058/

相关文章:

css - GWT:具有固定列大小的 Flextable

java - 在数据网格中以自定义字符分割行值

java - 将 Maven 构建生成的 SQL 脚本放在哪里?

java - 基于不同 equals/hashcode 的不同 Set 集合

java - JPanel 显示在 JFrame 右下角

java - 在不破坏封装的情况下保存和加载对象

javascript - Angular js : Unable to add marker in google map

javascript - 一个按钮——3个应用商店——如何将用户重定向到合适的应用商店?

javascript - 扁平化 Javascript 字典,保留嵌套键

java - GWT ServleContext 和检索文件