Java调用JavaScript

 

1.main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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" 
    
    <TextView   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="Welcome to Mr Wei's Blog." 
        /> 
    <WebView 
        android:id="@+id/webview" 
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
    /> 
    <Button 
        android:id="@+id/button" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Change the webview content" 
    /> 
</LinearLayout>

2.demo.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html
    <mce:script language="javascript"><!-- 
    
        function fillContent(){ 
            document.getElementById("content").innerHTML =  
                 "This Content is showed by Android invoke Javascript function."; 
        
       
// --></mce:script>   
  <body
    <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p
    <p id="content"></p
    <p>A Demo ----Android and Javascript invoke each other.</p
    <p>Author:Frankiewei</p
  </body
</html>

3.WebViewDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.tutor.webwiewdemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
public class WebViewDemo extends Activity {
    private WebView mWebView;
    private Button mButton;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupViews();
    }
    //初始化
    private void setupViews() {
        mWebView = (WebView) findViewById(R.id.webview);
        WebSettings mWebSettings = mWebView.getSettings();
        //加上这句话才能使用javascript方法
        mWebSettings.setJavaScriptEnabled(true);
        //增加接口方法,让html页面调用
        mWebView.addJavascriptInterface(new Object() {
            //这里我定义了一个打开地图应用的方法
            public void startMap() {
                Intent mIntent = new Intent();
                ComponentName component = new ComponentName(
                        "com.google.android.apps.maps",
                        "com.google.android.maps.MapsActivity");
                mIntent.setComponent(component);
                startActivity(mIntent);
            }
        }, "demo");
        //加载页面
        mWebView.loadUrl("file:///android_asset/demo.html");
        mButton = (Button) findViewById(R.id.button);
        //给button添加事件响应,执行JavaScript的fillContent()方法
        mButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mWebView.loadUrl("javascript:fillContent()");
            }
        });
    }
}

  

           

首界面                                               点击按钮时,html内容改变

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.jsdemo;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
 
public class MainActivity extends AppCompatActivity {
    private WebView wView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wView = (WebView) findViewById(R.id.wView);
        wView.loadUrl("file:///android_asset/demo1.html");
        WebSettings webSettings = wView.getSettings();
        //①设置WebView允许调用js
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        //②设置支持js调用java
        wView.addJavascriptInterface(new AndroidAndJSInterface(),"Android"");
    }
 
 class AndroidAndJSInterface{
        @JavascriptInterface
        public void showToast(){
            Toast.makeText(MainActivity.this"我被js调用了", Toast.LENGTH_SHORT).show();
        }
    }
}

注意:解决该WebView.addJavascriptInterface接口不起作用的两种办法

①针对版本改成16

②在JavaScript接口类的方法加上@JavascriptInterface注解

2.JavaScript调用Java对象示例

demo1.html

1
<input type="button" value="点击Android被调用" onclick="window.Android.showToast()" />

  

android 调用js,js调用android的更多相关文章

  1. [转]JS调用Android里面的方法,Android调用JS里面的方法

    FROM : http://blog.csdn.net/hj563308597/article/details/45197709 Android WebView 在公司Android的开发过程中遇到一 ...

  2. Android开发学习之路--Java和Js互相调用

      随着前端的火热,以前开发的快速,越来越多的native app在其中融合了h5,就拿淘宝就是很多的h5组成的,一旦出现什么节日,他都可以不用通过更新app来实现界面的改变,而且android和io ...

  3. Android与js互相调用

    有话要说: 本篇主要总结了简单的Android与js互相调用的方法. 在开发过程中遇到了需要在安卓中调用js方法的需求,于是将具体的实现过程总结成这篇博客. 效果: 其中“调用安卓方法”按钮是html ...

  4. iOS 或者Android调用vue.js 里面的方法

    1.原生调用vue.js 某个vue组件下的方法. 比如**.vue里面有个这样的方法: 如果这样的话,在iOS或者Android里面是调用不了这个ajax方法的. 需要在**.vue (我的版本是v ...

  5. android js 互相调用

    代码地址如下:http://www.demodashi.com/demo/13107.html android js 互相调用 第二版 支持js匿名函数接收 支持js json对象接收 支持js函数返 ...

  6. 【Android】java中调用JS的方法

    最近因为学校换了新的教务系统,想做一个模拟登陆功能,发现登陆的账号和密码有一个js脚本来进行加密 整理了一下java中执行JS的方法 智强教务 账号 密码 加密方法 var keyStr = &quo ...

  7. Android和JavaScript相互调用的方法

    转载地址:http://www.jb51.net/article/77206.htm 这篇文章主要介绍了Android和JavaScript相互调用的方法,实例分析了Android的WebView执行 ...

  8. Android-webview和js互相调用

    Android-webview和js互相调用 Android 和 H5 都是移动开发应用的非常广泛.市面上很多App都是使用Android开发的,但使用Android来开发一些比较复杂附属类,提示性的 ...

  9. WebView使用详解(一)——Native与JS相互调用(附JadX反编译)

    念念不忘,必有回响,永远坚持你所坚持的! 一直在用WebView,还没有系统的总结过它的用法,下面就系统的总结下,分享给大家 一.基本用法 1.加载在线URL void loadUrl(String ...

  10. Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式

    IOS:Objective-C 和 JavaScript 的相互调用 iOS7以前,iOS SDK 并没有原生提供 js 调用 native 代码的 API.但是 UIWebView 的一个 dele ...

随机推荐

  1. bash copy multi files

    bash copy multi files # copy one file $ cp file1.js /var/www/html # copy multi files ??? no space $ ...

  2. ESLint & vue

    ESLint & vue { "name": "app", "version": "1.0.1", " ...

  3. 微信小程序-云开发-实战项目

    微信小程序-云开发-实战项目 微信小程序 微信小程序平台服务条款 https://developers.weixin.qq.com/miniprogram/product/service.html h ...

  4. HOC in Depth

    HOC in Depth Higher-Order Components https://reactjs.org/docs/higher-order-components.html 1. wrappe ...

  5. React Hooks & React v16.8.6

    React Hooks Hooks are a new addition in React 16.8 const [state, setState] = useState(initialState); ...

  6. React & Dva & Actions & dispatch & effects

    React & Dva & Actions & dispatch & effects dispatch https://dvajs.com/guide/introduc ...

  7. 用WPF写了个处理视频,音频,图片的工具

    处理工具依赖ffmpeg,感兴趣可以看下Github上的源码 下载地址也在Github上

  8. 法兰西金融专访SPC空投重磅来袭

    最近,法兰西金融日报联合德意志财经等知名金融媒体就SPC这一话题进行了专访. 法兰西金融日报记者德维尔斯问到,之前2020年的BGV项目等市场反响异常火爆,2021年已经来到,NGK目前有何新的大动作 ...

  9. Laravel Queues 队列应用实战

    队列,顾名思义,排着队等着做事情.在生活场景中,凡是排队的人,都是带有目的性的.要完成某件事情,才去排队的,要不没有谁会闲到排队玩儿.而在软件应用层面,队列是什么,队列有什么优点,我们什么时候需要用队 ...

  10. 获取点击元素的id

    1.onclick="dianji(this.id)" 传入id到方法里function dianji(id){ //这个就是id}2. $(document).click(fun ...