使用 JSNI 在 GWT 中使用本机 Phonegape js

Use native Phonegape js in GWT using JSNI

我想要运行 GWT 上使用 JSNI 的 phonegap 本机警报

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 为 null 吗?否则请进一步查看我上面链接的那个 JSNI 文档。