Cordova4.3.1 android 自定義插件(顯示土司Toast)

自定義插件步驟:

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,做回調相關操作

 

 

 

 

 

 

 

 

 

 

 


 

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