android webview 添加内置对象
2014-01-26 11:21
295 查看
package com.android.EBrowser;
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.*;
public class EWebActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "WebActivity";
public WebView mWeb = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeb = (WebView) findViewById(R.id.webwiew);
//此处需要打开js 开关
mWeb.getSettings().setJavaScriptEnabled(true);
mWeb.setBackgroundColor(0);
///添加java js 内置对象。
mWeb.addJavascriptInterface(new Utility(), "Utility") ;
mWeb.clearCache(true);
mWeb.setInitialScale(100);
mWeb.requestFocus();
mWeb.loadUrl("http://172.23.65.145/index.htm");
}
///* js 对象实现。
class Utility {
////@JavascriptInterface 从android 4.2,之后,需要加上,否则js 运行后会找不到方法,
///这个需要注意。
@JavascriptInterface
public void setValue(String a,String b,String c,String d){
Log.d(TAG,"====>a="+a +"b="+b+"c="+c+"d="+d);
}
@JavascriptInterface
public String getValue(){
Log.d(TAG,"====>to get value");
return "eos";
}
}
//*/
}
//////////////////////////////////////////////////////////////////////////////////
//index.htm 页面实现。
<html>
<head>
</head>
<body >
hello world
<a id="a1" href="www.google.com" >google</a>
</br>
<script language="javascript">
Utility.setValue("11","22","33","55");
Utility.getValue();
</script>
</body>
</html>
////////////////////////////////////////////////////////////
(1), 关于 js 中调用内置对象 需要这样使用
Utility.setValue(); 此种扩展的js 内置对象不能,使用new Utility().setValue();
(2),为安全考虑,(js可以通过,反射机制去访问,修改webview 等)在android 4.2 以后。js 扩展接口需要加上,“@JavascriptInterface” ,
否则会报 Object [object Object] has no method 这个类错误。
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.*;
public class EWebActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "WebActivity";
public WebView mWeb = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWeb = (WebView) findViewById(R.id.webwiew);
//此处需要打开js 开关
mWeb.getSettings().setJavaScriptEnabled(true);
mWeb.setBackgroundColor(0);
///添加java js 内置对象。
mWeb.addJavascriptInterface(new Utility(), "Utility") ;
mWeb.clearCache(true);
mWeb.setInitialScale(100);
mWeb.requestFocus();
mWeb.loadUrl("http://172.23.65.145/index.htm");
}
///* js 对象实现。
class Utility {
////@JavascriptInterface 从android 4.2,之后,需要加上,否则js 运行后会找不到方法,
///这个需要注意。
@JavascriptInterface
public void setValue(String a,String b,String c,String d){
Log.d(TAG,"====>a="+a +"b="+b+"c="+c+"d="+d);
}
@JavascriptInterface
public String getValue(){
Log.d(TAG,"====>to get value");
return "eos";
}
}
//*/
}
//////////////////////////////////////////////////////////////////////////////////
//index.htm 页面实现。
<html>
<head>
</head>
<body >
hello world
<a id="a1" href="www.google.com" >google</a>
</br>
<script language="javascript">
Utility.setValue("11","22","33","55");
Utility.getValue();
</script>
</body>
</html>
////////////////////////////////////////////////////////////
(1), 关于 js 中调用内置对象 需要这样使用
Utility.setValue(); 此种扩展的js 内置对象不能,使用new Utility().setValue();
(2),为安全考虑,(js可以通过,反射机制去访问,修改webview 等)在android 4.2 以后。js 扩展接口需要加上,“@JavascriptInterface” ,
否则会报 Object [object Object] has no method 这个类错误。
相关文章推荐
- iOS 4的无线部署(Xcode3.2 及 Xcode4.2 图文解说)
- Android输入法挤乱布局问题
- 【翻译】Android Support Library Setup(三)
- android pbap client 蓝牙
- iOS上OCR SDK
- 在ios中的按比例图片裁剪(自动根据你的imageView裁剪图片)
- ubuntu下android源码的下载(最新)
- unity3d屏幕不休眠
- objective c输出控制符
- android 全屏 无标题栏
- appium搭建及要求---在 Mac 上配置 Appium
- Android学习笔记 第12章
- Android Design版微信的界面风格
- iOS6和iOS7代码的适配(5)——popOver
- Eclispe中的ArcGIS Android SDK更新地址发生改变
- Android中解析XML
- DetailView中改变需要的queryset和object
- android使用全局变量的两种方法
- android 如何修改默认时间制式格式
- Android Design in Action — 以微信为例