ToastPlugin插件在cordova3.3下使用

    自己开发的Todos应用,想实现“ 再按一次返回键退出程序 ”的功能,采用网上的ToastPlugins插件,发现代码或文章基本都是老版本,运行问题比较多。折腾了好久才弄好。下面吧基于cordova3.3下的ToastPlugins相关代码共享。
 
    ToastPlugin.java
package  com.hy.todos.plugins;
 
import  org.apache.cordova.CallbackContext;
import  org.apache.cordova.CordovaPlugin;
import  org.json.JSONArray;
import  org.json.JSONException;
 
import  android.util.Log;
import  android.widget.Toast;
 
public  class  ToastPlugin  extends  CordovaPlugin
{    
     @Override
     public  boolean  execute(String action, JSONArray data,  final  CallbackContext callbackContext)
    {
         int  tmpDuration;
        String tmpText =  "none" ;
        Log.i( "ToastPlugin"  ,  "ToastPlugin json data::"  +data);
         try
        {
            tmpText = data.getString(0);
        }
         catch  (JSONException e)
        {
            callbackContext.error(  "Parsing Text Exception" );
             return  false ;
        }
         try
        {
            tmpDuration = data.getInt(1);
        }
         catch  (JSONException e)
        {
            callbackContext.error(  "Parsing Duration Exception" );
             return  false ;
        }
 
        showMsg(tmpText,tmpDuration);
         return  true ;
    }
   
     public  synchronized  void  showMsg( final  String msg,  final  int  duration) {
           Runnable runnable =  new  Runnable() {
                  public  void  run() {  
                     Toast. makeText( cordova .getActivity(), msg, duration).show();
                     
                }
           };
             cordova .getActivity().runOnUiThread(runnable);
     }
   
}
 
Toast-Plugin.js
var  Toast=  function () {
};
 
Toast. prototype  .LENGTH_SHORT = 0;
Toast. prototype  .LENGTH_LONG = 1;
 
Toast. prototype  .show =  function  (msg, duration ,successCallback, failureCallback){
      return  cordova.exec(
           successCallback,   
           failureCallback,    
             'ToastPlugin' ,
             null ,             
           [msg, duration]       
     );
};
 
//Short Toast
Toast. prototype  .shortshow =  function  (msg,successCallback, failureCallback){    
      this .show(msg,  this .LENGTH_SHORT ,successCallback, failureCallback);
};
 
//Long Toast
Toast. prototype  .longshow =  function  (msg,successCallback, failureCallback){    
      this .show(msg,  this .LENGTH_LONG ,successCallback, failureCallback);
};
 
cordova.addConstructor( function () {
     if  (!window.plugins) {
           window.plugins = {};
     }
     window.plugins.toast =  new  Toast();
});
 
在deviceready事件中注册用户触发返回键的backbutton事件,在其中进行逻辑处理和调用插件代码:
var  quitflag = 0;
     
     document.addEventListener(  "deviceready" ,onDeviceReady,  false );
     function  onDeviceReady() {
           navigator.splashscreen.hide(); 
           document.addEventListener(  "backbutton" , eventBackButton,  false );  //注册返回键
    }
     function  eventBackButton(){
      if (window.plugins.toast){
           console.log(  "window.plugins.Toast......" );
           window.plugins.toast.shortshow(  "再按一次返回键退出程序"  );
     }
 
    quitflag++;
    setTimeout( function () {
                quitflag = 0;
           }, 3000);
 
             if  (quitflag >= 2) {
                navigator.app.exitApp();
           }    
    }
 
调试了好几次发现能正常按2此返回键退出代码,但确没有信心提示。最后发现是没有在config.xml中进行插件配置声明。晕!
< feature  name =  "ToastPlugin" >
    < param  name  = "android-package"  value =  "com.hy.todos.plugins.ToastPlugin"  />
</ feature  >
 
至此,ToastPlugin插件在Cordova3.3上完好运行!:)

你可能感兴趣的:(Cordova)