2013-05-17 35 views
3

我正在爲Android手機創建SMS插件。當我試圖發送消息時,我收到無效的操作錯誤。我曾看過Android + phonegap +package manager。我的問題是一樣的,但鏈接中的任何答案都無濟於事。手機短信插件中的操作錯誤無效

這是我的.js文件。

var SmsPlugin = function() {}; 

SmsPlugin.prototype.send = function (phone, message, successCallback, failureCallback) {  
    return cordova.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]); 
}; 

window.sms = new SmsPlugin(); 

而以下是我的java文件

package com.*.PhonegapSMSDPN; 

import org.apache.cordova.api.PluginResult; 
import org.apache.cordova.api.PluginResult.Status; 
import org.json.JSONArray; 
import org.json.JSONException; 

import android.app.PendingIntent; 
import android.content.Intent; 
import android.telephony.SmsManager; 
import android.util.Log; 
import android.widget.Toast; 

import org.apache.cordova.api.CordovaPlugin; 

public class SmsPlugin extends CordovaPlugin { 
    public final String ACTION_SEND_SMS = "SendSMS"; 

    public PluginResult execute(String action, JSONArray arg1, String callbackId) { 
     Toast.makeText(this.cordova.getActivity(), action, Toast.LENGTH_SHORT).show(); 
     PluginResult result = new PluginResult(Status.INVALID_ACTION); 
     Log.e("msg", action); 
     if (action.equals(ACTION_SEND_SMS)) { 
      try { 
       String phoneNumber = arg1.getString(0); 
       String message = arg1.getString(1); 
       sendSMS(phoneNumber, message); 
       result = new PluginResult(Status.OK); 
      } 
      catch (JSONException ex) { 
       result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage()); 
       Log.e("error", result+" "); 
      }   
     } 

     return result; 
    } 

    private void sendSMS(String phoneNumber, String message) { 
     SmsManager manager = SmsManager.getDefault(); 

     PendingIntent sentIntent = PendingIntent.getActivity(this.cordova.getActivity(), 0, new Intent(), 0); 

     manager.sendTextMessage(phoneNumber, null, message, sentIntent, null); 
    } 

} 

我已在config.xml文件中必要的修改了。 請指引我在解決problem.Thanks

PS-我使用Phonegap2.7.0

回答

0

你的js文件缺少一些代碼。我也在phonegap中實現了一個sms插件,這裏是我工作的js代碼。您應該自定義插件路徑。

var SmsPlugin = function() {}; 

SmsPlugin.prototype.send = function(phone, message, successCallback, failureCallback) { 
    return PhoneGap.exec(successCallback, failureCallback, 'SmsPlugin', "SendSMS", [phone, message]); 
}; 

PhoneGap.addConstructor(function() { 
    PhoneGap.addPlugin("sms", new SmsPlugin()); 
}); 

cordova.define("app/scripts/vendor/smsplugin", function (require, exports, module) { 
    var exec = require('cordova/exec'); 
    var SmsPlugin = function() {}; 

    /** 
    * Check if the device has a possibility to send and receive SMS 
    */ 
    SmsPlugin.prototype.isSupported = function (successCallback, failureCallback) { 
     return exec(successCallback, failureCallback, 'SmsPlugin', 'HasSMSPossibility', []); 
    } 
    /** 
    * Send a message to the given phone number 
    */ 
    SmsPlugin.prototype.send = function (phone, message, successCallback, failureCallback) { 
     return exec(successCallback, failureCallback, 'SmsPlugin', 'SendSMS', [phone, message]); 
    } 

    var smsplugin = new SmsPlugin(); 
     module.exports = smsplugin; 
});