Cordova Android 插件開發(網絡端(服務器)調用Android插件(java))

之前寫了一篇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是調用了,但插件不調用。

現在調用插件在頁面裏調用就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章