详见:
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. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)TextView 动态设置Drawable
  2. Android(安卓)-- 获取IP和MAC地址
  3. android 获取路径目录方法
  4. android 字符串国际化的问题
  5. Android(安卓)中the connection to adb i
  6. Android(安卓)驱动与应用
  7. android 背景圆角以及图片圆角处理
  8. Android控件开发之四----ListView(2)
  9. Android(安卓)ImageView、TextureView、
  10. Android(安卓)Studio对于Java8特性的支持