现在的位置: 首页 > 综合 > 正文

Phonegap插件编写

2013年02月23日 ⁄ 综合 ⁄ 共 2461字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/avenleft/article/details/7383294

目前PhoneGap虽然提供了很多的API,但是我们也很容易发现,他有很多地方不能满足我们的需求,比如发短信。我也将介绍如何开发一个针对PhoneGap发短信的Plugin。(Android 版本)。

1、创建一个Android工程,创建之后的目录结构如图:

2、把PhoneGap的android版jar包、javascript和xml文件夹加入工程。(参照上一篇中的做法)目录结构:

3、新增一个类MessagePlugin
这个类就是负责发送短信功能的具体类,可以调用发送短信的android API,当然如果是其他功能,则调用相应的API,但是当使用这个API时记得申请权限。
具体代码:

Java代码 复制代码 收藏代码
  1. package com.phonegap.kiddyu.plugin; 
  2.  
  3. import org.json.JSONArray; 
  4. import org.json.JSONException; 
  5. import org.json.JSONObject; 
  6.  
  7. import android.telephony.SmsManager; 
  8.  
  9. import com.phonegap.api.Plugin; 
  10. import com.phonegap.api.PluginResult; 
  11.  
  12. public class MessagePlugin extends Plugin
  13.      
  14.     private static final String
    SEND = 
    "send"
  15.      
  16.     @Override 
  17.     public PluginResult
    execute(String action, JSONArray data, String callbackId) { 
  18.         PluginResult result; 
  19.         if (SEND.equals(action))
  20.             try { 
  21.                 JSONObject jsonObj = new JSONObject(); 
  22.                 String target = data.getString(0); 
  23.                 String content = data.getString(1); 
  24.                 SmsManager sms = SmsManager.getDefault(); 
  25.                 sms.sendTextMessage(target, null,
    content, 
    nullnull); 
  26.                 jsonObj.put("target",
    target); 
  27.                 jsonObj.put("content",
    content); 
  28.                 result = new PluginResult(PluginResult.Status.OK,
    jsonObj); 
  29.             } catch (JSONException
    ex) { 
  30.                 result = new PluginResult(PluginResult.Status.JSON_EXCEPTION); 
  31.             }catch(IllegalArgumentException
    ex){ 
  32.                 result = new PluginResult(PluginResult.Status.ERROR); 
  33.             } 
  34.         } else { 
  35.             result = new PluginResult(PluginResult.Status.INVALID_ACTION); 
  36.         } 
  37.  
  38.         return result; 
  39.     } 
  40.  



4、为phonegap增加一个javascript的API
在工程目录www/js下面增加一个javascript文件:phonegapPlugin.js,然后加入代码:

Javascript代码 复制代码 收藏代码
  1. var Message
    function(){}; 
  2.  
  3. Message.prototype = { 
  4.         send: function(success,
    error, target, content){ 
  5.             PhoneGap.exec(success, error, "MessagePlugin""send",
    [target, content]); 
  6.         } 
  7. }; 
  8.  
  9. PhoneGap.addConstructor(function()
  10.     PhoneGap.addPlugin("message"new Message()); 
  11. }); 
[javascript] view
plain
copy

  1. var Message = function(){};  
  2.   
  3. Message.prototype = {  
  4.         send: function(success, error, target, content){  
  5.             PhoneGap.exec(success, error, "MessagePlugin""send", [target, content]);  
  6.         }  
  7. };  
  8.   
  9. PhoneGap.addConstructor(function() {  
  10.     PhoneGap.addPlugin("message"new Message());  
  11. });  



关键的方法是PhoneGap.exec(success, error, "MessagePlugin", "send", [target, content]);

抱歉!评论已关闭.