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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Welcome to Mr Wei's Blog."
     />
<WebView
  android:id="@+id/webview"
  android:layout_width="fill_parent"
     android:layout_height="wrap_content"
/>
<Button
  android:id="@+id/button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Change the webview content"
/>
</LinearLayout>

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

<html>
<mce:script language="javascript"><!--

function fillContent(){
   document.getElementById("content").innerHTML =
        "This Content is showed by Android invoke Javascript function.";
  }

// --></mce:script> 
  <body>
<p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>
<p id="content"></p>
<p>A Demo ----Android and Javascript invoke each other.</p>
<p>Author:Frankiewei</p>
  </body>
</html>

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

package com.tutor.webwiewdemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

public class WebViewDemo extends Activity {
private WebView mWebView;
private Button mButton;
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  setupViews();
}
//初始化
private void setupViews() {
  mWebView = (WebView) findViewById(R.id.webview);
  WebSettings mWebSettings = mWebView.getSettings();
  //加上这句话才能使用javascript方法
  mWebSettings.setJavaScriptEnabled(true);
  //增加接口方法,让html页面调用
  mWebView.addJavascriptInterface(new Object() {
   //这里我定义了一个打开地图应用的方法
   public void startMap() {
    Intent mIntent = new Intent();
    ComponentName component = new ComponentName(
      "com.google.android.apps.maps",
      "com.google.android.maps.MapsActivity");
    mIntent.setComponent(component);
    startActivity(mIntent);
   }
  }, "demo");
  //加载页面
  mWebView.loadUrl("file:///android_asset/demo.html");
  mButton = (Button) findViewById(R.id.button);
  //给button添加事件响应,执行JavaScript的fillContent()方法
  mButton.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    mWebView.loadUrl("javascript:fillContent()");
   }
  });
}
}

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

    

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

点击html的startGoogleMap启动地图应用

Android中通过WebView控件实现与JavaScript方法相互调用的地图应用的更多相关文章

  1. android中的EditView控件

    android中的EditView控件 EditText继承关系:View-->TextView-->EditText ,EditText是可编辑文本框 1.EditText默认情况下,光 ...

  2. C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用)

    C#中的BackgroundWorker控件+Delegate.Invoke (委托同步调用) 简单代码,记录一下.一个BackgroundWorker控件  backgroundWorkerRefr ...

  3. 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法

    一.我们先在XML当中自定义一个webview(Second_layout.xml) 代码如下: <?xml version="1.0" encoding="utf ...

  4. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...

  5. Android中自定义IP控件

    最近在搞Android项目,之前并没有系统的去学过这方面的编程,只能边看书边撸代码.在项目的开发的过程中,需要一个IP控件,后面了解到Android中并没有这样的控件,于是网上搜索,发现得到的结果并不 ...

  6. android中去掉ListView控件中的分割线

    通过设置android:divider="@null" ,可以去掉ListView控件中的分割线 也可以自定义分割线的颜色,比如: <ListView android:id= ...

  7. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  8. Android中动态改变控件的大小的一种方法

    在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...

  9. Android中的常用控件之进度条(ProgressBar)

    ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...

随机推荐

  1. MockMvc和Mockito之酷炫使用

    由于项目中需要添加单元测试,所以查询之后发现Mockito非常适合现在的web项目. 首先需要添加pom依赖: <dependency> <groupId>junit</ ...

  2. 利用原生JavaScript获取样式的方式小结

    来源:http://www.ido321.com/930.html ps:是获取样式,不是设置样式.若没有给元素设置样式值,则返回浏览器给予的默认值.(论坛整理) 1.element.style:只能 ...

  3. 高精度+搜索+质数 BZOJ1225 [HNOI2001] 求正整数

    // 高精度+搜索+质数 BZOJ1225 [HNOI2001] 求正整数 // 思路: // http://blog.csdn.net/huzecong/article/details/847868 ...

  4. Java每日一则-001

    Java中类名与文件名的关系 1.Java保存的文件名必须与类名一致: 2.如果文件中只有一个类,文件名必须与类名一致: 3.一个Java文件中只能有一个public类: 4.如果文件中不止一个类,文 ...

  5. YARN应用场景、原理与资源调度

    1.Hadoop YARN产生背景 源于MapReduce1.0 运维成本 如果采用“一个框架一个集群”的模式,则可能需要多个管理员管理这些集群,进而增加运维成本,而共享模式通常需要少数管理员即可完成 ...

  6. java计时代码

    public class Test{ public static void main(String[] args) { long startMili=System.currentTimeMillis( ...

  7. 解决问题的步骤(第一篇)-- clwu

    现象: 之前打开IE 还是正常的,但前几天开始打开就不正常了,报错如下. 处理(别人的)问题的步骤: 百度一下  0xc0000018,没有什么有用信息. 看一下程序(IE)启动时做了些什么. 怎么看 ...

  8. Todolist

    UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)

  9. homework-06&homework-09

    homework-06 1) 把程序编译通过, 跑起来 , 把正确的 playPrev(GoMove) 的方法给实现了. public void playPrev(GoMove gm) { // ne ...

  10. HDU 5680 zxa and set (数学 推导结论)

    zxa and set 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/G Description zxa has a set , ...