自定义插件步骤:
1,自定义Toast插件类
package com.hl.hello.myPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
/**
* 显示土司插件
* @author yuhailong
*
*/
public class Toast extends CordovaPlugin {</p><p>
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
if("showToast".equals(action)){
showToast(args.getString(0), args.getInt(1));
}
callbackContext.success();
return true;
}</p><p> private void showToast(String text,int type){
CordovaInterface cordova = this.cordova;
if(type==1){
android.widget.Toast.makeText(cordova.getActivity(), text, 1).show();
}else{
android.widget.Toast.makeText(cordova.getActivity(), text, 0).show();
}
}
}
</p>
2,在res/xml/config.xml文件中增加插件配置
<feature name="WebToast">
<param name="android-package" value="com.hl.hello.myPlugin.Toast"/>
</feature>
3,在asserts/www/plugins/目录下插件自己的插件js文件
如:asserts/www/plugins/com.hl.hello.myPlugin/toast.js
<p>cordova.define("com.hl.hello.myPlugin.Toast", function(require, exports, module) { /*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* <a target=_blank href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/</p><p>var exec = require('cordova/exec');</p><p>/**
* Provides access to notifications on the device.
*/</p><p>module.exports = {</p><p>
/**
* Causes the device to beep.
* On Android, the default notification ringtone is played "count" times.
*
* @param {Integer} type The Toast type.
*/
showToast: function(content,type) {
exec(null, null, "WebToast", "showToast", [content,type]);
}
};</p><p>});
</p>
4,添加土司插件js配置信息
在assets/www/cordova_plugins.js文件中添加如下信息:
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
{
"file": "plugins/org.apache.cordova.geolocation/www/geolocation.js",
"id": "org.apache.cordova.geolocation.geolocation",
"clobbers": [
"navigator.geolocation"
]
},
//增加插件js位置描述及调用对象 ,调用时将通过 navigator.webtoast.showToast()方法显示土司
<span style="color:#ff6666;">{
"file": "plugins/com.hl.hello.myPlugin/toast.js",
"id": "com.hl.hello.myPlugin.Toast",
"clobbers": [
"navigator.webtoast"
]
}</span>
];
});
5,代码调用
<p>var myToast = {</p><p> showToast: function(text,type){
navigator.webtoast.showToast(text,type);
}
};</p><p>myToast.showToast("你好,我是Toast.",1);</p>
toast.js中的方法解释:
exec(null, null, "WebToast", "showToast", [content,type]);
1,成功回调
2, 失败回调
3,插件名称 在res/xml/config.xml文件中指定
4,方法名称
5,方法参数
Toast.java类中的方法解释:
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
1,方法名称 对应exec 参数4
2,方法参数集合 对应exec 参数5
3,做回调相关操作