android 与HTM5 互调入门例子
来源:互联网 发布:战天堂翅膀进阶数据 编辑:程序博客网 时间:2024/05/22 00:35
如开发是使用android stdio开发的话,需要参考
http://blog.csdn.net/lsyz0021/article/details/51473194
该文章建人建相应的目录和文件,直接上内容了,创建index.html,内容如下:
<!doctype html><html><head> <meta charset="utf-8"> <title>学习</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes"/> <script type="text/javascript"> function jsconsole(){ document.getElementById("div_1").innerHTML="hello world"; } </script></head><body><a>学习了................................</a><input type="button" value="点击Android被调用" onclick="window.android.toast('abc')" /><div id="div_1"></div></body></html>
加截WebView 的activity Layout文件,文件名activity_web_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/tab_bg" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="callJs" android:id="@+id/button22" /> </LinearLayout></LinearLayout>
activity 代码如下:
package com.example.thinkpad.myapplication; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.util.Log; import android.view.View; import android.view.Window; import android.webkit.JavascriptInterface; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.Toast; import com.alibaba.fastjson.*; import com.peidw.beans.Stud;public class WebViewActivity extends BasicActivity { private WebView webview; private Button button22; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_web_view); button22=(Button)findViewById(R.id.button22); webview=(WebView)findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(new JSBridge(), "android"); //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webview.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view,String url){ //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } }); webview.loadUrl("file:///android_asset/index.html"); button22.setOnClickListener(new Button.OnClickListener(){//创建监听 public void onClick(View v) { webview.loadUrl("javascript:jsconsole()" ); } }); } public class JSBridge{ @JavascriptInterface public String toast(String str) { Toast.makeText(getApplicationContext(), "传入的参数是" + str, Toast.LENGTH_SHORT).show(); return "我是android信息"; } @JavascriptInterface public void call(String phone) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)); startActivity(intent); } }}
还可以能考:
http://www.jianshu.com/p/a25907862523
0 0
- android 与HTM5 互调入门例子
- Android app混合开发,本地应用与htm5交互
- android开发之htm5交互(一)
- 自己理解接口回调入门
- HTM5规范
- htm5 特性
- HTM5:拖放
- HTML5开发岗位猛增44% iOS增速不及Android-htm5来了
- htm5 页面跳转在android出现的奇葩问题 【已解决】
- maven调入依赖包
- 动态调入xib视图
- Htm5 坦克大战
- HTM5 Basic<1>
- HTM5之WEB Worker
- htm5笔记-web存储
- HTM5学习前篇
- htm5 本地存储
- htm5文件上传预览
- JProfiler9.2使用说明
- Equivalent Sets 【至少加几边使图为强联通】+【 scc +缩点】
- Android 中java反射应用(三)——应用场景及总结
- 从零开始搭建Raspberry Pi机器视觉编程环境
- Uber的合作创始人告诉我们:人工智能如何改变世界
- android 与HTM5 互调入门例子
- Meeting Room Arrangement
- 物联网时代哪三种职业最吃香?--转自美国科技博客TechCrunch撰稿人扎克·苏帕拉(Zach Supalla)
- [动态规划] 洛谷P1063 能量项链 (石子合并)
- 嵌入式开发者技能大全
- HTML5开发者心声:浏览器兼容性成最大问题
- 动态规划-最长公共子序列
- Notes on tensorflow(八)read tfrecords with slim
- 【杂谈】解决Windows系统下上传速度慢的一个小技巧