Android与JavaScript方法相互调用

来源:互联网 发布:电影天堂软件 编辑:程序博客网 时间:2024/05/29 19:28

在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用android方法,在这里我个人觉得有点和DWR相似。

为了让大家容易理解,我写了一个简单的Demo,具体步骤如下:

第一步:新建一个Android工程,命名为WebViewDemo(这里我在assets里定义了一个html页面)。

第二步:修改main.xml布局文件,增加了一个WebView控件还有Button控件,代码如下:

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="Welcome to Mr Wei's Blog."
  11. />
  12. <WebView
  13. android:id="@+id/webview"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:text="Change the webview content"
  22. />
  23. </LinearLayout>

第三步:在assets目录下新建一个demo.html文件,代码如下(这里不知道为何多了mce:这几个东东,<script></script>这样是对的):

[css] view plaincopy
  1. <html>
  2. <mce:script language="javascript"><!--
  3. function fillContent(){
  4. document.getElementById("content").innerHTML =
  5. "This Content is showed by Android invoke Javascript function.";
  6. }
  7. // --></mce:script>
  8. <body>
  9. <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>
  10. <p id="content"></p>
  11. <p>A Demo ----Android and Javascript invoke each other.</p>
  12. <p>Author:Frankiewei</p>
  13. </body>
  14. </html>

第四步:修改主核心程序WebViewDemo.java,代码如下:

[java] view plaincopy
  1. package com.tutor.webwiewdemo;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.webkit.WebSettings;
  8. import android.webkit.WebView;
  9. import android.widget.Button;
  10. publicclass WebViewDemoextends Activity {
  11. private WebView mWebView;
  12. private Button mButton;
  13. publicvoid onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. setupViews();
  17. }
  18. //初始化
  19. privatevoid setupViews() {
  20. mWebView = (WebView) findViewById(R.id.webview);
  21. WebSettings mWebSettings = mWebView.getSettings();
  22. //加上这句话才能使用javascript方法
  23. mWebSettings.setJavaScriptEnabled(true);
  24. //增加接口方法,让html页面调用
  25. mWebView.addJavascriptInterface(new Object() {
  26. //这里我定义了一个打开地图应用的方法
  27. publicvoid startMap() {
  28. Intent mIntent =new Intent();
  29. ComponentName component =new ComponentName(
  30. "com.google.android.apps.maps",
  31. "com.google.android.maps.MapsActivity");
  32. mIntent.setComponent(component);
  33. startActivity(mIntent);
  34. }
  35. },"demo");
  36. //加载页面
  37. mWebView.loadUrl("file:///android_asset/demo.html");
  38. mButton = (Button) findViewById(R.id.button);
  39. //给button添加事件响应,执行JavaScript的fillContent()方法
  40. mButton.setOnClickListener(new Button.OnClickListener() {
  41. publicvoid onClick(View v) {
  42. mWebView.loadUrl("javascript:fillContent()");
  43. }
  44. });
  45. }
  46. }

第五步:运行上述工程,查看效果。

首界面 点击按钮时,html内容改变

点击html的startGoogleMap启动地图应用

0 0
原创粉丝点击