惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
AI
AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
Cisco Talos Blog
Cisco Talos Blog
T
Tenable Blog
L
Lohrmann on Cybersecurity
Know Your Adversary
Know Your Adversary
A
Arctic Wolf
T
Threatpost
AWS News Blog
AWS News Blog
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cisco Blogs
F
Full Disclosure
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
I
InfoQ
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
Intezer
C
CERT Recently Published Vulnerability Notes
Last Week in AI
Last Week in AI
P
Privacy International News Feed
Scott Helme
Scott Helme
WordPress大学
WordPress大学
Simon Willison's Weblog
Simon Willison's Weblog
D
Docker
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Y
Y Combinator Blog
T
Tor Project blog
V
Visual Studio Blog
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Google Online Security Blog
Google Online Security Blog
C
Check Point Blog
爱范儿
爱范儿
N
News and Events Feed by Topic
博客园 - 【当耐特】
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - 努力&快乐

springboot 手动开启事务及手动提交事务 js MD5包含中文串时加密结果与JAVA结果不一致的解决方案 mongodb配置、启动、备份 JavaScript通过递归合并JSON linux系统关闭指定服务的方式 Dart 语言简易教程系列 查看java内存情况命令 JAVA用QRCode生成二维码 安装redis时Newer version of jemalloc required错误解决 代码指标工具 spring mvc接收ajax提交的JSON数据,并反序列化为对象 二进制样式的字符串与byte数组互转函数示例 .net运行时dll的查找路径顺序 WebStorm11 注册 MSSql使用SQL语句快速查看表对的就说明,及表字段描述及字段类型 多个App间传递数据 cordova plugin数据传递概要 javascript不用new关键字创建对象示例 .Net事件管道详解图
cordova混合开发:Android中native调用javascript
努力&快乐 · 2016-03-29 · via 博客园 - 努力&快乐

今天学习怎么在java中调用javascript方法,做个记录:

第一种方式,这个最简单:

loadUrl("javascript:func1()");

要注意要在deviceready后调用,否则会报方法未定义的错误:"Uncaught ReferenceError: fun1 is not defined";

第二种方式:注册一个通道,在native中向js发送回调,这也是新版cordova推荐的方法:

javascript:

function myinit(){
    alert('12355');
}

//最省事的就是找个现成的插件添加个函数"callJSInit",能执行注册就可以,或者在cordova_plugins.js里注册一个单独的插件模块,见注1;
cordova.define("插件模块ID", function(require, exports, module) {
var exec = require('cordova/exec');
    var callJS = {
    init:function() {
        cordova.require('cordova/channel').onCordovaReady.subscribe(function(){
            exec(succeedCallback, null, "PluginName", "callJSInit", []);
            function succeedCallback(message){
        //执行js代码
        eval(message);
        //还可以这样,大胆的发挥你的想象力
        /*
        if(message=='f1')
            alert(1);
        else if(message == 'f2')
            alert(2);
        ……
        */         
            }
        });
        }
    };

    module.exports = callJS;
});

//注1:在cordova_plugins.js里注册一个单独的插件模块
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
        //添加:
        {
            "file": "js文件路径",
            "id": "插件模块",
            "clobbers": ["navigator.callJS"]
        }
    ]
}


//在app deviceready后执行:
navigator.callJS.init()

java文件:

public class PluginName extends CordovaPlugin {
    private static CallbackContext mCallbackContext;

    @Override
    public boolean execute(String action, final JSONArray args,
            final CallbackContext callbackContext) throws JSONException {
        if (action.equals("callJSInit")) {
            mCallbackContext = callbackContext;    //拿到回调对象并保存
            //PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "calljs init ready");
            //dataResult.setKeepCallback(true);// 非常重要
            //mCallbackContext.sendPluginResult(dataResult);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Object onMessage(String id, Object data) {
        return null;
    }

    public static void callJS(String message) {
        if (mCallbackContext != null) {
            PluginResult dataResult = new PluginResult(PluginResult.Status.OK, message);
            dataResult.setKeepCallback(true);// 非常重要
            mCallbackContext.sendPluginResult(dataResult);
        }
    }
}

在其它java文件中调用js:

PluginName.callJS("myinit()");

运行app就会显示"myinit"方法中的内容了。

//在app deviceready后执行: