3

我正在为 Android 中的 phonegap 创建一个 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 文件中进行了必要的更改。请指导我解决问题。谢谢

PS-我正在使用Phonegap2.7.0

4

1 回答 1

0

您的 js 文件缺少一些代码。我也在 phonegap 中实现了一个短信插件,这是我的工作 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;
});
于 2013-08-11T10:06:30.327 回答