webview中的js调用native的实例
来源:互联网 发布:java类的访问权限 编辑:程序博客网 时间:2024/06/05 01:18
一、首先简单的布局文件main.xml,主要有两个控件我们主要研究webview控件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<WebView
android:id="@+id/appView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
--------------------------------------------------------------------
二、定义的activity文件MainActivity.java:
package com.liu;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.webkit.WebView;
import android.annotation.SuppressLint;
import android.app.Activity;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView appView;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
appView = (WebView) findViewById(R.id.appView);
//appView = new WebView(this);
appView.getSettings().setJavaScriptEnabled(true);
appView.addJavascriptInterface(new PluginMethod(this, appView), "SM"); // 注意这里一句
//appView.setIntegerProperty("splashscreen", R.drawable.ic_launcher);
appView.loadUrl("file:///android_asset/www/accountInfo.html");
}
}
其中
1、appView.addJavascriptInterface(new PluginMethod(this, appView), "SM"); // 注意这里一句
是主要注册给js调用本地方法的接口
2、appView.loadUrl("file:///android_asset/www/accountInfo.html");
这句主要是加载本地的html文件,
-----------------------------------------------------------------------------------------
三、本地被js调用的函数及其方法PluginMethod.java:
package com.liu;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.DroidGap;
import android.content.Context;
import android.util.Log;
import android.webkit.WebView;
public class PluginMethod {
Context droidGap;
WebView webView;
public PluginMethod(Context droidGap, WebView appView) {
this.droidGap = droidGap;
this.webView = webView;
}
/**
* JS调用 用于更新App
* @param path 更新门店的地址
*/
public void UpdateApp(final String path) {
Log.e("---------------", path); //注意这里日志输出
}
}
-----------------------------------------------------------------
四、本地的html文件accountInfo.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<title>Hello World</title>
<style type="text/css" >body,div,p{font-size:28px;line-height:150%;}</style>
</head>
<body>
账号公司信息demo:
<br />
<input type="button" value="读取账号信息" onclick="window.SM.UpdateApp('http://192.168.0.1');"/>
<div id="info"></div>
</body>
</html>
这样运行这个程序,则点击按钮就会调用本地的java方法
- webview中的js调用native的实例
- Android WebView js与Java调用实例
- Android中webview js与native方法互相调用
- android webview不能调用JS中的function?
- react native js中调用native中的方法
- webView 实现 与 javascript调用java方法(也称js调用native 方法) helloworld
- webView 实现 与 javascript调用java方法(也称js调用native 方法) helloworld
- WebView的简单使用,JS的调用
- WebView中JS的双向调用
- WebView调用本地的js,css,html
- Android webview和js的相互调用
- android与js的调用(WebView)
- WebView调用JS失败
- webview调用js
- WebView/JS/Java调用
- webview调用js注意事项
- webView中调用js
- webview调用js
- Android 2D Graphics学习(二)、Canvas篇2、Canvas裁剪和Region、RegionIterator
- 进程与内存6-高速缓存1(每CPU页框高速缓存和内存高速缓存)
- svn merge 用法
- Esper学习之六:EPL语法(二)
- 内存篇之堆泄漏
- webview中的js调用native的实例
- Jdom读取XML文件
- Android SDK更新失败问题解决
- 第五届蓝桥杯——基础练习:字母图形
- SSH框架一步一步搭建
- ParseInt函数参数设置
- (android实战)实现【关于我们】功能(textview的文本样式几种实现方式)
- 进程与内存7-高速缓存2(目录项高速缓存、索引节点高速缓存和磁盘高速缓存)
- Android Xmpp 黑名单