Cordova Android 插件开发(网络端(服务器)调用Android插件(java))

22 篇文章 0 订阅
5 篇文章 0 订阅

之前写了一篇Cordova插件的开发,但只是简单的放在Android本地www目录下的html调用的

本地html调用插件地址
地址:http://blog.csdn.net/aierjun/article/details/53908957

因需求要我们的服务器调用我的插件,所以和前端妹子协作,弄了半天,终于是弄出来了,总结下,希望对你们有用。

好了,思想+代码

Android端代码:

插件代码:

public class SavePlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        LogUtils.LogUtils("exec.........");
        if ("save".equals(action)){
//            save(callbackContext);
            Context Activity = this.cordova.getActivity().getApplicationContext();
            SharedPreferencesUtils.save(Activity,200,"goback");
            LogUtils.LogUtils("save..............");
        }
        return true;
    }

    private  synchronized void save(CallbackContext callbackContext) {
        Context Activity = this.cordova.getActivity().getApplicationContext();
        SharedPreferencesUtils.save(Activity,200,"goback");
        LogUtils.LogUtils("save..............");
        callbackContext.success();
    }
}

Config.xml配置代码:

<feature name="Save">
        <param name="android-package" value="com.aierjun.test.myPlugin.SavePlugin" />/>
    </feature>

Save.js配置代码:

cordova.define("cordova-plugin-save.Save", function(require, exports, module) { 

var exec = require('cordova/exec');

var save = {
    save:function() {
        exec(null, null, "Save", "save", []);
    }
};

module.exports = save;

});

注意:此处的Save.js是自己创建的,创建图片如下:

路径如下

Cordova_plugins.js配置代码:


cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
{
"file": "plugins/cordova-plugin-splashscreen/www/splashscreen.js",
"id": "cordova-plugin-splashscreen.SplashScreen",
"pluginId": "cordova-plugin-splashscreen",
"clobbers": [
"navigator.splashscreen"
]
},
{
"file": "plugins/cordova-plugin-app-version/www/AppVersionPlugin.js",
"id": "cordova-plugin-app-version.AppVersionPlugin",
"pluginId": "cordova-plugin-app-version",
"clobbers": [
"cordova.getAppVersion"
]
},
{
"file": "plugins/cordova-plugin-save/www/Save.js",
"id": "cordova-plugin-save.Save",
"pluginId": "cordova-plugin-save",
"clobbers": [
"cordova.save"
]
}
];
module.exports.metadata =
// TOP OF METADATA
{
"cordova-plugin-splashscreen": "4.0.1",
"cordova-plugin-app-version": "0.1.9",
"cordova-plugin-save":"1.0.0"
}
// BOTTOM OF METADATA
});

注意:此处第三个才是自己的配置,前两个是我用的官网的插件的配置。

关于配置详解,请看这篇文章的配置说明,地址:http://blog.csdn.net/it_talk/article/details/50790826

点击自动跳转地址

index.xml配置代码:

<html xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<script>
fuction res(){
cordova.save.save();
}
</script>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
<p class="button" onclick="res()">Check!</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>

至此Android端已配置好。

现在要让服务器访问写的插件的方法就是将刚刚写的WWW的目录整个放到服务器上,放在你的服务器的工程下,运行服务器这个www也会运行,然后访问你的服务器下的www目录下的index.html测试,看页面是否是你访问Cordova默认的页面。loadUrl(服务器地址/www/index.html);

如果测试访问没问题的话,将你的工程里要调用插件的网页加上这两句

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/cordova_plugins.js"></script>

注意:src的地址都指向www目录下的这两个地址,这两个地址最好不要换到其他地方,我们试过换位置,结果这两个js是调用了,但插件不调用。

现在调用插件在页面里调用就可以了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值