Android JAVA和JS的通信
来源:互联网 发布:假装情侣聊天软件 编辑:程序博客网 时间:2024/06/05 18:27
//目录结构
//效果
//一起来看下代码
//index.html HTML页面
<html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK" /><script>function showInfo(str){document.getElementById("info").innerHTML=str;}</script></head><body>重Android传来的值是:<span id="info"> </span><br><a href="#" onclick="window.android.callAndroid('你好,Android')">call Android from javascript</a></body></html>
//main.xml主布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/sendtojs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="发送数据到JS" /> <TextView android:id="@+id/fromjsdata" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <EditText android:id="@+id/sendText" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <WebView android:id="@+id/web" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
//JSJAVAActivity.java JAVA控制代码
package sn.len.jsjava;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class JSJAVAActivity extends Activity implements OnClickListener{private WebView web_webView;private Button but_sendToJs;private EditText edi_sendText;private TextView tex_fromjsdata;private Handler handler = new Handler();@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);web_webView=(WebView)findViewById(R.id.web);edi_sendText=(EditText)findViewById(R.id.sendText);but_sendToJs=(Button)findViewById(R.id.sendtojs);tex_fromjsdata=(TextView)findViewById(R.id.fromjsdata);but_sendToJs.setOnClickListener(this);//开启访问JS代码功能web_webView.getSettings().setJavaScriptEnabled(true);//根据地址加载网页//文件头: file:////文件路径: /android_asset/index.htmlweb_webView.loadUrl("file:///android_asset/index.html");//参数1,把new SendToJs()这个对象构建到WEB浏览器中//参数2,提供在WEB浏览器中好让javascript访问的对象//例如: 在WEB中,javascript调用callAndroid(value)方法写法为window.android.callAndroid('value');web_webView.addJavascriptInterface(new SendToJs(), "android");}@Overridepublic void onClick(View v){//向浏览器发送数据,调用JS的showInfo函数web_webView.loadUrl("javascript:showInfo('"+edi_sendText.getText().toString()+"')");}class SendToJs{public void callAndroid(final String arg){//利用Handler更新UIhandler.post(new Runnable(){@Overridepublic void run(){//设置重JS发过来的数据tex_fromjsdata.setText("重javascript发来的数据是:"+arg);}});}}}
- Android JAVA和JS的通信
- Android JAVA和JS的通信
- Android WebView Java和JS通信
- Android和js通信
- Android Java与JS的通信
- Android和JS通信方案
- Android WebView Java与Js通信
- android的NDK和java进行本地socket通信
- android的NDK和java进行本地socket通信
- android的NDK和java进行本地socket通信
- Android java和C的Socket通信demo(可用)
- Js WebScoket与Java WebSocket的通信
- js和Flash的通信原理
- JS与ANDROID通信
- android native C 和 java 通信
- android native C 和 java 通信
- Android与java和.NET通信
- android socket java和C通信
- 国行2.2详细教程,root-删除系统程序-unroot,已发附件
- 黑马程序员_网络编程
- 测试覆盖率
- c# 验证码识别基础类(含源码)
- QGraphicsView 中不能刷新的解决方案
- Android JAVA和JS的通信
- C++中奇怪的虚函数
- Java SE 6 Hotspot [TM] 虚拟机垃圾回收调优
- MyEclipse自动补全与快捷键设置
- Oracle中临时表的深入研究
- 蓦然回首 那足球 2011
- Memcached缓存技术
- JSON and Servlet example
- PL SQL基本语法要术