Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

android - Adding JavaScript Interface to a Browser

Is it possible to add a JavaScript interface to the Android Browser the same way one can be added to the WebView Component as illustrated in this demo. My particular use case only needs JavaScript -> android so that I can send it back to the previous activity.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can invoke methods and functions in your webview by using javascript url's, e.g.

webview.loadUrl("javascript:somemethod()");

You will, of course, need to enable javascript on your webview:

webview.getSettings().setJavaScriptEnabled(true);

This is from java to javascript. If you want to invoke java code / android API's from javascript, use addJavascriptInterface()

webview.addJavascriptInterface(new MyJSJavaBridge(), "api");

All of this is shown in the example url you posted as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...