本篇文章介绍的内容是关于android 与js的简单交互的代码,现在分享给大家,有需要的朋友参考一下
权限:
93635dec8fe53503e36cabacb84f1940715e355a41f150f13c9dfb3c5419088c
mainacticity:
import android.content.dialoginterface;
import android.os.bundle;
import android.support.v7.app.alertdialog;
import android.support.v7.app.appcompatactivity;
import android.view.view;
import android.view.viewgroup;
import android.webkit.jsresult;
import android.webkit.webchromeclient;
import android.webkit.websettings;
import android.webkit.webview;
import android.webkit.webviewclient;
import android.widget.button;
/**
* 注意事项:如何避免webview内存泄露?
* 不在xml中定义 webview ,而是在需要的时候在activity中创建,并且context使用 getapplicationgcontext()
* <p>
* 在 activity 销毁( webview )的时候,
* 先让 webview 加载null内容,然后移除 webview,再销毁 webview,最后置空
*/
public class mainactivity extends appcompatactivity implements view.onclicklistener {
private webview web;
private button but;
private button but2;
private websettings settings;
private button but3;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
but = findviewbyid(r.id.but);
but2 = findviewbyid(r.id.but2);
but3 = findviewbyid(r.id.but3);
web = findviewbyid(r.id.web);
but.setonclicklistener(this);
but2.setonclicklistener(this);
but3.setonclicklistener(this);
//声明websettings子类
settings = web.getsettings();
//如果访问的页面中要与javascript交互,则webview必须设置支持javascript
settings.setjavascriptenabled(true);
//设置自适应屏幕,两者合用
settings.setusewideviewport(true); //将图片调整到适合webview的大小
settings.setloadwithoverviewmode(true); // 缩放至屏幕的大小
// 设置与js交互的权限
settings.setjavascriptenabled(true);
// 设置允许js弹窗
settings.setjavascriptcanopenwindowsautomatically(true);
// 先载入js代码
// 复写shouldoverrideurlloading()方法,使得打开网页时不调用系统浏览器, 而是在本webview中显示
web.setwebviewclient(new webviewclient() {
@override
public boolean shouldoverrideurlloading(webview view, string url) {
view.loadurl(url);
return true;
}
});
// 由于设置了弹窗检验调用结果,所以需要支持js对话框
// webview只是载体,内容的渲染需要使用webviewchromclient类去实现
// 通过设置webchromeclient对象处理javascript的对话框
//设置响应js 的alert()函数(回调方法)
web.setwebchromeclient(new webchromeclient() {
@override
public boolean onjsalert(webview view, string url, string message, final jsresult result) {
alertdialog.builder b = new alertdialog.builder(mainactivity.this);
b.settitle("alter");
b.setmessage(message);
b.setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog, int which) {
result.confirm();
}
});
b.setcancelable(false);
b.create().show();
return true;
}
});
}
@override
public void onclick(view view) {
switch (view.getid()) {
//获取本地html文件
case r.id.but:
web.loadurl("file:///android_asset/index.html");
break;
//与js交互
case r.id.but2:
web.post(new runnable() {
@override
public void run() {
// 格式规定为:file:///android_asset/文件名.html
web.loadurl("file:///android_asset/webview.html");
web.loadurl("javascript:calljs()");
}
});
break;
case r.id.but3:
web.loadurl("http://www.baidu.com/");
break;
}
}
//销毁
@override
protected void ondestroy() {
if (web != null) {
web.loaddatawithbaseurl(null, "", "text/html", "utf-8", null);
web.clearhistory();
((viewgroup) web.getparent()).removeview(web);
web.destroy();
web = null;
}
super.ondestroy();
}
}
xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.webview_js.mainactivity">
<button
android:id="@+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我调用本地html文件" />
<button
android:text="点我调用网站"
android:id="@+id/but3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<button
android:id="@+id/but2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我与js交互" />
<webview
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"></webview>
</linearlayout>
html文件(js):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function calljs(){
alert("android调用了js的call方法")
}
</script>
</head>
<body>
</body>
</html>
相关推荐:
ajax()与后台交互步骤详解
android与js的交互之jsbridge使用
ajax+php的数据交互实现
以上就是android 与js的简单交互的详细内容。