Phonegap-2.2.0的word Plugin插件编写

这次要做一个基于phonegap和sencha touch的文件浏览器,在编写word、ppt、pdf的插件时,碰到了问题,发现是版本的问题,phonegap-2.0以后对插件编写有了一些改变。我在网上找了很多phonegap插件编写方法,也看了官方的document,发现都是phonegap低版本的编写方法,对phonegap-2.0以后插件编写资料不是很多,经过一番努力,终于将插件完成。我将word插件写法做个例子,供大家相互学习。

         1.  在所在包下新建一个Java类,取名docPlugin.java。在docPlugin中导入以下包:

                   importorg.apache.cordova.api.CallbackContext;
                   importorg.apache.cordova.api.CordovaPlugin;
                   importorg.json.JSONArray;
                   importorg.json.JSONException;
                   importandroid.content.Intent;
                   import android.net.Uri;

         将docPlugin类继承CordovaPlugin(这就与低版本的有区别),并重写execute函数(这个也与低版本的有区别),(activity跳转的方法也有修改)修改完代码如下:

           

import java.io.File;

 

importorg.apache.cordova.api.CallbackContext;

importorg.apache.cordova.api.CordovaPlugin;

import org.json.JSONArray;

import org.json.JSONException;

 

import android.content.Intent;

import android.net.Uri;

 

public class docPlugin extends CordovaPlugin {

 

        publicstatic final String ACTION = "showDoc";

 

        @Override

        publicboolean execute(String action, JSONArray args,

                       CallbackContext callbackContext) throws JSONException{

 

               Stringresult = "";

               if(ACTION.equals(action)) {

 

                       Stringmessage = args.getString(0);

                       this.callback(message,callbackContext);

 

                       result= this.showDoc(args.getString(0));

                       if(result.length() > 0) {

                               returntrue;

                       }

               }

               returnfalse;

        }

 

 

public String showDoc(StringfileName) {

               FiledocFile = new File(fileName);

 

               Stringtype = "";

               Stringend = fileName.substring(fileName.lastIndexOf(".") + 1,

                               fileName.length());

               if(end.equals("doc")) {

                       type= "application/msword";

               }else if (end.equals("docx")) {

                       type="application/vnd.openxmlformats-officedocument.wordprocessingml.document";

               }

 

               try{

                       Uripath = Uri.fromFile(docFile);

                       Intentintent = new Intent(Intent.ACTION_VIEW);

                       intent.setDataAndType(path,type);

                       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                       this.cordova.getActivity().startActivity(intent);

                       return"";

               }catch (android.content.ActivityNotFoundException e) {

                       System.out.println("docPlugin:Error loading url " + fileName + ":"

                                      +e.toString());

                       returne.toString();

               }

        }

 

        privatevoid callback(String message, CallbackContext callbackContext) {

               if(message != null && message.length() > 0) {

                       callbackContext.success(message);//这里的succsee函数,就是调用js的success函数

               }else {

                       callbackContext.error("Expectedone non-empty string argument.");

               }

        }

 

}

      

        写好Java代码后,就要开始写js的接口了。

      2.  js的接口编写,在assets/www/的目录下建一个libs文件夹,在里面新建一个docPlugin.js,里面内容如下:

      

function docPlugin() {

    }

 

    docPlugin.prototype.showDoc =function(content) { 

       

        return cordova.exec(success,failed,"docPlugin","showDoc", [content]); // 关键方法:"docPlugin" 该参数一定要与config.xml中的类名一致
  //exec的参数依次是  回调函数,执行失败的回调函数, xml中注册的插件名,插件中用于判断的Action名,参数                                                                           

//"showDoc" 该参数是action参数,是docPlugin中action参数

    };

 

    function success(message){

        alert("success"+message);

    }

   

    function failed(message){

        alert("failed"+message);

    }

    cordova.addConstructor(function() {

        if (!window.plugins) {

            window.plugins = {};

        }

        window.plugins.docplugin = new docPlugin();  //addPlugin方法取消后用这种方式创建插件

    });

 

    3.  完成js接口以后,在res/xml/config.xml中插入以下内容

<pluginname="docPlugin" value="com.huatusoft.myPhoneGap.docPlugin"/>//value的内容是该类的具体路径

 

  4. 页面调用,在index.html中添加以下语句

<scripttype="text/javascript" src="libs/docPlugin.js"charset="UTF-8"></script>

 
调用的方式 window.plugins.docplugin.showDoc(docPath);   docPath是doc文件的路径。注意:phonegap的路径是 :file:///sdcard  而我们要传入的路径是:/sdcard


好了,这样就ok了,其他ppt、pdf等等啦,方法都一样的。  水平有限,切勿吐槽!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值