在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. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...

  2. HDU 5762 Teacher Bo

    Teacher Bo Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  3. KVO KVC

    @interface FoodData : NSObject { NSString * foodName; float foodPrice; } @end ////////////////////// ...

  4. ASP.NET常用加密解密方法

    ASP.NET常用加密解密方法 一.MD5加密解密 1.加密 C# 代码           public static string ToMd5(string clearString)        ...

  5. ubuntu下php5扩展mysqli

    看网上说的都是自己编译源码,试了一下其实这样就可以了 sudo apt-get instal php5-mysql

  6. Eclipse中设置在创建新类时自动生成注释的方法

     windows–>preference Java–>Code Style–>Code Templates code–>new Java files 编辑它 ${filecom ...

  7. 测试xss和xsf

    xss注入攻击: 123123123 被动注入: 1231231231231231 主动注入: 对不起,你需要登录才能评论 用户名 密码

  8. Aggregation(1):Blending、Bagging、Random Forest

    假设我们有很多机器学习算法(可以是前面学过的任何一个),我们能不能同时使用它们来提高算法的性能?也即:三个臭皮匠赛过诸葛亮. 有这么几种aggregation的方式: 一些性能不太好的机器学习算法(弱 ...

  9. HDU-4751 Divide Groups 染色问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识. ...

  10. javascript !!作用

    javaScript中使用!!表示取得boolean值,具体作用如下 var value= !!test[1]; 取变量的Boolean值, 相当于 var value = test[1]?true: ...