Phonegap: calling a java function from Javascript - Stack Overflow

I want to call one of my Java functions in Javascript and get its result. In order to do that I followe

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 .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

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 .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

Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Oct 23, 2012 at 12:21 D3GAND3GAN 64010 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I remend 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 munication. 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./en/2.1.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide

In phonegap i remend 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();
})();

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().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745122495a4612494.html

相关推荐

  • Phonegap: calling a java function from Javascript - Stack Overflow

    I want to call one of my Java functions in Javascript and get its result. In order to do that I followe

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信