[英]Calling Java Method from Javascript in Android Project - PhoneGap
[英]Phonegap: calling a java function from Javascript
我想在Javascript中調用我的Java函數之一並獲得其結果。 為了做到這一點,我遵循了本教程和這個問題 。 我一步一步地跟隨他們,但仍然出現此錯誤
無法調用未定義的方法“ 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”
我建議您編寫一個PhoneGap插件,而不要嘗試使用自己的方法。 我們已經經歷了JavaScript與Java通信的所有痛點。 使用我們已經編寫的內容,您不會碰到過去三年來我們已經解決的Android錯誤。
在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();
})();
我也在JavascriptInterface
苦苦掙扎。 不能調用showKeyboard
的原因是恕我直言,您應該調用window.showKeyBoard()
而不是window.Keyboard.showKeyBoard()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.