简体   繁体   English

Phonegap:从Javascript调用Java函数

[英]Phonegap: calling a java function from Javascript

I want to call one of my Java functions in Javascript and get its result. 我想在Javascript中调用我的Java函数之一并获得其结果。 In order to do that I followed this tutorial and this question . 为了做到这一点,我遵循了本教程这个问题 I followed them step by step and I still get this error 我一步一步地跟随他们,但仍然出现此错误

Cannot call method 'showKeyBoard' of undefined 无法调用未定义的方法“ showKeyBoard”

Here is my java class: 这是我的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);
  }
}

Here is my Main class: 这是我的主班:

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;
    }
  }

And I call it in Javascript like this: 我用Javascript这样称呼它:

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

Is there anything that I haven't done or am missing? 有什么我没做过或想念的吗? As I said I get this error: 正如我所说的,我得到这个错误:

Cannot call method 'showKeyBoard' of undefined 无法调用未定义的方法“ showKeyBoard”

I recommend that you write a PhoneGap plugin instead of trying to roll your own method. 我建议您编写一个PhoneGap插件,而不要尝试使用自己的方法。 We've already gone through all the pain points of the JavaScript to Java communication. 我们已经经历了JavaScript与Java通信的所有痛点。 Use what we've already written and you won't run into the Android bugs that we've already smoothed over in the past 3 years. 使用我们已经编写的内容,您不会碰到过去三年来我们已经解决的Android错误。

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

In phonegap i recommend you using a custom plugin do this but still if you want to make a direct call to Java see this example to get a general idea 在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";

        }
    }

in javascript 在JavaScript中

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

@JavascriptInterface annotation is required in you code to make this work . 在您的代码中需要@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);
  }
}

And in Javascript call it like this: 在Javascript中这样称呼它:

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

I'm struggling with JavascriptInterface too. 我也在JavascriptInterface苦苦挣扎。 The reason why you cant call showKeyboard is IMHO you should call window.showKeyBoard() instead of window.Keyboard.showKeyBoard() . 不能调用showKeyboard的原因是恕我直言,您应该调用window.showKeyBoard()而不是window.Keyboard.showKeyBoard()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM