Phonegap:从Javascript调用java函数

时间:2012-10-23 12:21:33

标签: java javascript android cordova

我想在Javascript中调用我的一个Java函数并获得它的结果。为此,我按照this tutorialthis question进行了操作。我一步一步地跟着他们,我仍然得到这个错误

  

无法调用未定义的方法'showKeyBoard'

这是我的java类:

package keyboard;
import org.apache.cordova.DroidGap;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;

public class KeyBoard {
  private WebView mAppView;
  private DroidGap mGap;
  public KeyBoard(DroidGap gap, WebView view) {
    mAppView = view;
    mGap = gap;
  }
  public void showKeyBoard() {
    InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
    // only will trigger it if no physical keyboard is open
    mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
    ((InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mAppView, 0);
  }
  public void hideKeyBoard() {
    InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(mAppView.getWindowToken(), 0);
  }
}

这是我的主要课程:

package com.example.helloworld;
import keyboard.KeyBoard;
import android.os.Bundle;
import org.apache.cordova.*;
import android.view.Menu;
import QR.*;

public class MainActivity extends DroidGap {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    KeyBoard keyboard = new KeyBoard(this, appView);
    appView.addJavascriptInterface(keyboard, "KeyBoard");
    super.loadUrl("file:///android_asset/www/index.html");
  }

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
    }
  }

我用Javascript这样称呼它:

(function(){
  window.KeyBoard.showKeyBoard();
})();

有什么我没有做过或丢失的事吗?正如我所说,我得到了这个错误:

  

无法调用未定义的方法'showKeyBoard'

3 个答案:

答案 0 :(得分:2)

我建议你write a PhoneGap plugin而不是尝试推广自己的方法。我们已经经历了JavaScript到Java通信的所有难点。使用我们已经编写的内容,您将不会遇到我们在过去3年中已经平滑过的Android错误。

http://docs.phonegap.com/en/2.1.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide

答案 1 :(得分:1)

在phonegap中,我建议您使用自定义插件执行此操作 但是如果你想直接调用Java,请参阅这个例子以获得一般性的想法

public class MainActivity extends DroidGap {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setIntegerProperty("loadUrlTimeoutValue", 70000);
            super.loadUrl("file:///android_asset/www/index.html");
            super.appView.addJavascriptInterface(new Bridge(), "b");
        }
    }

    class Bridge {
        @JavascriptInterface
        public String a()
        {
            Log.i("Bridge","This is from js");
            return "This is a message";

        }
    }

在javascript中

setTimeout(function(){
      alert(b.a());
}, 1000);

代码中需要@JavascriptInterface注释才能使其正常工作。

package keyboard;
import org.apache.cordova.DroidGap;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;

public class KeyBoard {
  private WebView mAppView;
  private DroidGap mGap;
  public KeyBoard(DroidGap gap, WebView view) {
    mAppView = view;
    mGap = gap;
  }
  /*make it visible in bridge*/
   @JavascriptInterface
  public void showKeyBoard() {
    InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
    // only will trigger it if no physical keyboard is open
    mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
    ((InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mAppView, 0);
  }
  /*make it visible in bridge*/
   @JavascriptInterface
  public void hideKeyBoard() {
    InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(mAppView.getWindowToken(), 0);
  }
}

在Javascript中称之为:

(function(){
  KeyBoard.showKeyBoard();
})();

答案 2 :(得分:0)

我也在与JavascriptInterface斗争。你不能拨打showKeyboard的原因是恕我直言,你应该拨打window.showKeyBoard()而不是window.Keyboard.showKeyBoard()