Reputation: 640
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
Upvotes: 0
Views: 9793
Reputation: 3998
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();
})();
Upvotes: 1
Reputation: 1
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()
.
Upvotes: 0
Reputation: 23273
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.
Upvotes: 2