首页 技术 正文
技术 2022年11月13日
0 收藏 589 点赞 2,255 浏览 3201 个字

一、首先是网页端,这个就是一些简单的标签语言和JS函数:

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>H5 And Android</title>
<script>
//定义本地方法 效果提供给Android端调用 被调用后将获得参数值
function callH5(data){
  document.getElementById(“result”).innerHTML=”result success for Android to:”+data;
}

//定义本地点击事件 效果调用Android方法 传递参数给Android客服端
function myOnclick(){
  document.getElementById(“result”).innerHTML=”按钮被点击了”
  //调用android本地方法 调用对象由Android定义实例化,调用方法由Android提供 (具体对象名和方法待定,可变更)
  myObj.callAndroid(“弹窗显示回调成功了~~~”);
}

js调用android

 注册js对象名称为control,方法为toNews,传的是新闻id和新闻类型  

WebSettings webSettings = webView.getSettings();
// 设置WebView属性,能够执行Javascript脚本
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
// 设置支持缩放
webSettings.setBuiltInZoomControls(true);
webView.addJavascriptInterface(new JavaScriptInterfaces(), "control");
// 加载需要显示的网页
webView.loadUrl(url + "?newsId=" + newsId + "&newsType=" + newsType + "&subjectImg=" + imageUrl);
String str = url + "?newsId=" + newsId + "&newsType=" + newsType + "&subjectImg=" + imageUrl;
LogUtils.e(url + "?newsId=" + newsId + "&newsType=" + newsType + "&subjectImg=" + imageUrl);
// 设置Web视图
webView.setWebViewClient(new webViewClient());// 如果去掉webView会用浏览器加载网络连接
class JavaScriptInterfaces {
JavaScriptInterfaces() {
} @JavascriptInterface
public void toNews(long newsId, String accessUrl) {
Intent intent = new Intent(SpecialWebActivity.this, NewsDetailsWebActivity.class);
intent.putExtra("newsId", newsId + "");
startActivity(intent);
}
}

</script>
</head>
<body>
  <button id=”button” onclick=”myOnclick()”>点击调用Android方法</button>
  <p/>
  <div id=”result”>效果展示</div>
</body>
</html>

二、接下来就是Android中加载web网页,并且设置完成交互了,直接上代码,也有详细注释:

package com.lvyerose.h5andandroid;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);

  mWebView = (WebView) findViewById(R.id.webView);
  initWeb();

}

@SuppressLint({“JavascriptInterface”, “SetJavaScriptEnabled”})
void initWeb(){
  //设置编码  
  mWebView.getSettings().setDefaultTextEncodingName(“utf-8”);
  //支持js
  mWebView.getSettings().setJavaScriptEnabled(true);

  //设置本地调用对象及其接口
  //第一个参数为实例化自定义的接口对象 第二个参数为提供给JS端调用使用的对象名
  mWebView.addJavascriptInterface(new Contact() {

@JavascriptInterface //必须加的注解
@Override
public void callAndroid(String phone) {
  Toast.makeText(MainActivity.this, phone, Toast.LENGTH_LONG).show();
}
}, “myObj”);
  //载入js
  mWebView.loadUrl(“http://lvyerose.com/h5andAndroid.html”);

  findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
  //Android调用Js方法,其中参数 ‘Android OK !!!’ 可传入变量 效果如下:
  // mWebView.loadUrl(“javascript:callH5(‘”+str+”‘)”);
  mWebView.loadUrl(“javascript:callH5(‘Android OK !!!’)”);

}
});
}
//定义接口,提供给JS调用
interface Contact {
  @JavascriptInterface
  void callAndroid(String phone);

}

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731