Android WebView 与 javascript交互

 有些时候为了偷懒或者业务需求,App里有时候是套网页的,好比PC 版的 QQ音乐库。点击网页里的播放为啥软件就会响应呢?道理是一样的,只不过现在主角是 Android 而已。

    webView 和 javascript 交互,也就是相互调用对方的方法,也就叫做 混合开发,是不是瞬间感觉高大上了。刚开始随意百度了一下,都是坑人的东西,各种代码不全有问题。谷歌

之后才看明白了,其实用起来很简单。

首先我们需要一个html5代码。这里我自己先建一个 html5 页面:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
			
			<script type="text/javascript">
				
				function showMsg (msg){
					alert(msg)
				}
				
			</script>
			
		<title></title>
	</head>
	<body>

		<button onClick="window.demo.clickOnAndroid()">调用Android方法</button>
	</body>
</html>

 activity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.webview_javascript.MainActivity" >

    
    <Button android:id="@+id/showJsMethod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示js方法"/>
    
    <Button android:id="@+id/shuaxin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="刷新网页"/>

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </WebView>

</LinearLayout>
MainActivity.java

package com.example.webview_javascript;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

	private WebView web;
	private Button showJsMethod;
	private Button shuaxin;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		web = (WebView) findViewById(R.id.webView);
		showJsMethod = (Button) findViewById(R.id.showJsMethod);
		shuaxin = (Button) findViewById(R.id.shuaxin);

		shuaxin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				web.loadUrl("http://192.168.0.2:8020/HelloHBuilder/index.html");
			}
		});

		showJsMethod.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				web.loadUrl("javascript:showMsg(\"我是Android调用的\")");

			}
		});

		WebSettings webSettings = web.getSettings();
		web.setWebChromeClient(new WebChromeClient() {
		});
		webSettings.setJavaScriptEnabled(true);
		webSettings.setSupportZoom(false);
		web.loadUrl("http://192.168.0.2:8020/HelloHBuilder/index.html");
		web.addJavascriptInterface(new DemoJavaScriptInterface(this, web),
				"demo");
		web.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				// 在这里执行你想调用的js函数
				Log.i("", "web_网页加载完成");
			}
		});
	}

}

DemoJavaScriptInterface.java

package com.example.webview_javascript;

import android.content.Context;
import android.os.Handler;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

public class DemoJavaScriptInterface {

	private Context context;
	private WebView web;
	private Handler mHandler = new Handler();
	
    DemoJavaScriptInterface(Context context,WebView web) {
    	this.context = context;
    	this.web = web;
    }

    /**
     * This is not called on the UI thread. Post a runnable to invoke
     * loadUrl on the UI thread.
     */
    @JavascriptInterface
    public void clickOnAndroid() {
    	Toast.makeText(context, "点击了", Toast.LENGTH_SHORT).show();
    	
    	

    }
}

注意 : web.addJavascriptInterface(new DemoJavaScriptInterface(this, web),"demo");

上面的demo 可以说是把 接口开放给js 并且起个名字, js里 就可以 <button onClick="window.接口名字.你的方法名">调用Android方法</button>  来调用,一般情况下,如果想要获取 js 之后过后的返回值,可以通过js调用java方法传回来。如果发现alert 弹不出来 请确保 你是 在 网页加载完成后调用的。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android WebView 是一个能够在 Android 应用程序中嵌入网页的组件,它可以开发出能够与 Web 页面进行交互的应用。其中与 Web 页面进行交互的一种方法是与 JavaScript 进行交互。下面简要解释一下 Android WebView 与 JS 交互的方式。 1. 加载本地 HTML 文件 在 Android WebView 中加载本地 HTML 文件时,需要使用 loadUrl() 方法加载。HTML 文件中的 JavaScript 可以通过 WebView 提供的 addJavascriptInterface() 方法注册为 Java 中的一个对象,然后在 Java 中调用该对象的方法,即可实现 JS 与 Java 的交互。 2. 加载远程 Web 页面 在 Android WebView 中加载远程 Web 页面时,需要添加 WebViewClient 和 WebChromeClient,分别是用来管理 WebView 的网络请求和处理页面上的 JavaScript 弹窗等请求。 在远程 Web 页面上,JS 代码可以通过 WebView 提供的 addJavascriptInterface() 方法注册为 Java 中的一个对象,然后在 Java 中调用该对象的方法,即可实现 JS 与 Java 的交互。 同时,在 Android 中处理 JS 的事件需要通过 JavaScriptInterface 向 WebView 注册一个映射对象,来实现 JS、Java 相互调用的机制,静态 HTML 文件是通过 WebView 中的 evaluateJavascript() 方法来调用 JS,来实现双向通信和数据交互。 总结来说,Android WebView 与 JS 交互的方式主要是通过 WebView 提供的 addJavascriptInterface() 方法注册为 Java 中的一个对象,然后在 Java 中调用该对象的方法,来实现 JS 和 Java 的交互。同时,JS 也可以通过 WebView 的 evaluateJavascript() 方法来调用 Java 中的方法,实现双向通信和数据交互

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值