0

I want to call one of my Java functions in Javascript and get its result. 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

Here is my java class:

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:

(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

Community
  • 1
  • 1
D3GAN
  • 642
  • 10
  • 26

3 Answers3

2

I recommend that you write a PhoneGap plugin instead of trying to roll your own method. We've already gone through all the pain points of the JavaScript to Java communication. 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.

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

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • 1
    I read it! but still I can't make it work in the javascript I can't call cordova.exec it said that "Uncaught ReferenceError: cordova is not defined " what should I do?how declare cordova for it? – D3GAN Oct 23 '12 at 15:27
  • 1
    Are you making sure you wait for the "deviceready" event before you call your method? API calls using cordova.exec will not work until "deviceready" fires. – Simon MacDonald Oct 23 '12 at 17:49
  • @SimonMacDonald I also have the same problem. tried executing the script after deviceready event. still the issue "undefined is not a function cordova exec" – Vishnudev K Jan 19 '15 at 09:05
1

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

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

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

@JavascriptInterface annotation is required in you code to make this work .

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:

(function(){
  KeyBoard.showKeyBoard();
})();
Midhun
  • 3,850
  • 1
  • 23
  • 26
0

I'm struggling with JavascriptInterface too. The reason why you cant call showKeyboard is IMHO you should call window.showKeyBoard() instead of window.Keyboard.showKeyBoard().

Matthieu
  • 16,103
  • 10
  • 59
  • 86