Android JAVA和JS的通信
2012-01-12 14:43
375 查看
//目录结构
//效果
//一起来看下代码
//index.html HTML页面
//main.xml主布局
//JSJAVAActivity.java JAVA控制代码
//效果
//一起来看下代码
//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(); @Override public 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.html web_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"); } @Override public void onClick(View v) { //向浏览器发送数据,调用JS的showInfo函数 web_webView.loadUrl("javascript:showInfo('"+edi_sendText.getText().toString()+"')"); } class SendToJs { public void callAndroid(final String arg) { //利用Handler更新UI handler.post ( new Runnable() { @Override public void run() { //设置重JS发过来的数据 tex_fromjsdata.setText("重javascript发来的数据是:"+arg); } } ); } } }
相关文章推荐
- Android WebView js 与 java 之间相互通信
- Android Java与JS的通信
- React Native Android 源码框架浅析(主流程及 Java 与 JS 双边通信)
- Android WebView 中 JS 与Java之间通信
- android中java与js通信(可以用html来做页面,进行交互)
- Android JAVA和JS的通信
- android中java与js通信(可以用html来做页面,进行交互)
- Android WebView的js中调用java代码
- 初学者---Android 中的js与java的交互
- Android React Native原生模块与JS模块通信的方法总结
- Android&Java解析JS
- android java与webview中js交互
- android webView js方法和java 方法交互
- android之js与java互相调用
- android webview一篇文章全面了解(基本使用,url拦截,js跟java交互)
- Java后台与js前端之间的通信问题
- Android与H5交互(java与js的交互)
- Android WebView JS不能调用Java对象的问题
- Android WebView JS和java交互
- 在Android中java与js互调的demo