phonegap短信插件中的操作错误无效

时间:2013-05-17 10:45:27

标签: android cordova sms phonegap-plugins

我正在为Android中的phonegap创建一个短信插件。我尝试发送邮件时收到无效的操作错误。我看过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文件中进行了必要的更改。 请指导我解决问题。谢谢

PS-i正在使用Phonegap2.7.0

1 个答案:

答案 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;
});