详见:
http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html#android-web-app-or-android-hybrid-app

Calling From Android Web App to JavaScript

It is also possible to call JavaScript functions inside the WebView from your Android web app. You have two possibilities to do so. Both will be covered below.

Calling JavaScript via WebView loadUrl()

Before API level 19 (before Android 4.4 - Kitkat) you can use the WebView loadUrl() method like this:

webView.loadUrl("javascript:theFunction('text')");
This has the same effect as clicking on a JavaScript link inside the page currently loaded in the WebView. It does not result in a new page being loaded. Rather it results in the JavaScript being executed within the currently loaded page.

The disadvantage of this method is that you cannot get any return values from the called function. However, you can arrange for the called JavaScript function to call back into Java with the result (how to call Java from JavaScript is explained earlier in this tutorial).

Calling JavaScript via WebView evaluateJavascript()

The second option is only available from Android API level 19 (Android Kitkat) and forward, Android's WebView class contains a method called evaluateJavascript(). This method can execute JavaScript as if it was executed inside the page currently loaded into the WebView . Here is an example of executing JavaScript via WebView evaluateJavascript() :

webView.evaluateJavascript("fromAndroid()", new ValueCallback () {
@Override
public void onReceiveValue(String value) {
//store / process result received from executing Javascript.
}
});
The first parameter passed to evaluateJavascript() is the JavaScript string to evaluate (execute). The second parameter is a callback object which contains a single method named onReceiveValue. When the JavaScript has been evaluated and a result obtained from it, the onReceiveValue() method of this callback object is called. The Android web app can then process the value returned from exeuting the JavaScript.

Keeping Page Navigation Inside the WebView With a WebViewClient

The the users clicks a link in the web page loaded into the WebView, the default behaviour is to load that URL of the link in the system Android browser. That means that the Android browser app is opened and the page for the link is shown in the Android browser, and not inside the WebView in your app. This breaks the user experience of your app's users.

To keep page navigation within the WebView and hence within your app, you need to create a subclass of WebViewClient, and override its shouldOverrideUrlLoading(WebView webView, String url) method. Here is how such a WebViewClient subclass could look:

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
return false;
}
}
When the shouldOverrideUrlLoading() method returns false, the URLs passed as parameter to the method is loaded inside the WebView instead of the Android standard browser. In the above example all URls will be loaded inside the WebView.

If you want to distinguish between that URLs are loaded inside the WebView and which are loaded in the Android browser, your implementation of shouldOverrideUrlLoading() can examine the URL passed to it as parameter. Here is an example that only loads URLs that contains jenkov.com inside the WebView and all other URLs in the Android browser:

public class WebViewClientImpl extends WebViewClient {

@Overridepublic boolean shouldOverrideUrlLoading(WebView webView, String url) {    if(url.indexOf("jenkov.com") > -1 ) return false;    return true;}

}
Weirdly enough, returning true from shouldOverrideUrlLoading() does not cause the URL to be loaded in the external Android browser. Rather, it causes the URL not to be loaded at all. To open all other URLs in the external Android browser you will have to fire an Intent. Here is how the WebViewClient subclass looks with that added:

public class WebViewClientImpl extends WebViewClient {

private Activity activity = null;public WebViewClientImpl(Activity activity) {    this.activity = activity;}@Overridepublic boolean shouldOverrideUrlLoading(WebView webView, String url) {    if(url.indexOf("jenkov.com") > -1 ) return false;    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));    activity.startActivity(intent);    return true;}

}
Notice how the WebViewClientImpl class now takes an Activity in its constructor. This activity is used to fire the Intent which opens the URL in the Android browser.

更多相关文章

  1. Android(Lollipop/5.0) Material Design(二) 入门指南
  2. Apache Hadoop 3.1.0 正式发布,原生支持GPU和FPGA
  3. Linux运维入门教程06-01 (系统的初始化和服务)
  4. android 3.1以上boot complete receiver接受的问题
  5. android设置ImageView边框
  6. Android开发笔记(十七)GIF动画的实现GifAnimation
  7. Android(安卓)EditText禁止输入中文字符
  8. Android中sqllite存储海量数据解决办法
  9. android的开发----环境搭配(原创)

随机推荐

  1. Android XML解析学习——Pull方式
  2. 代码混淆—android被反编译的两种解决方
  3. Android零基础教程8天学会移动开发
  4. H5 通过 input 标签,调起 Android 手机相
  5. Android的开机启动流程概述
  6. Android中输出HTML格式下的文字
  7. android之自定义控件一控件的呈现机制
  8. 用百度API高仿微信定位demo
  9. [置顶] Android(安卓)listview checkbox
  10. (android/swig实现)用c/c++混合编程方式