我一直在开发一个小应用程序,它应该在 web 视图中调用 javascript,而 js 应该回调一个 java 方法。我的目标是拥有
- - - - - - - 结果
打印了,但我从来没有看到它!我的代码很简单:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView wv;
private Object obj = new Object() {
@JavascriptInterface
public void onLogin() {
System.out.println("------------- result ");
}
};
private String jsString = "var checkLogin = function() {" +
"window.AndroidJSBridge.onLogin();" +
"}; " +
"checkLogin();";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("------------- start");
wv = (WebView) findViewById(R.id.login_webview);
wv.addJavascriptInterface(obj, "AndroidJSBridge");
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadData("", "text/html", null);
wv.loadUrl("javascript: " + jsString);
}
}