0

如何使用 Cordova/Phonegap 3.1 发送短信?

我已经搜索过,但所有教程/插件似乎都已过时,示例不起作用。

4

2 回答 2

1

在这里我分享我的工作短信插件。请试试这个。我希望这能帮到您。如果有任何问题让我知道。

短信插件.java

package org.apache.cordova.plugin;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;

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

    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals(ACTION_SEND_SMS)) {
            try {               
                String phoneNumber = args.getString(0);
                String message = args.getString(1);
                String method = args.getString(2);

                if(method.equalsIgnoreCase("INTENT")){
                    invokeSMSIntent(phoneNumber, message);
                    callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.NO_RESULT));
                } else{
                    sendSMS(phoneNumber, message);
                }

                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                return true;
            }
            catch (JSONException ex) {
                callbackContext.sendPluginResult(new PluginResult( PluginResult.Status.JSON_EXCEPTION));
            }           
        }
        return false;
    }

    private void invokeSMSIntent(String phoneNumber, String message) {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", message);
        sendIntent.setType("vnd.android-dir/mms-sms");
        this.cordova.getActivity().startActivity(sendIntent);
    }

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

索引.html

$(document).ready(function() {
             $("#btnDefaultSMS").click(function(){             
                var number = $("#numberTxt").val();
                var message = $("#messageTxt").val();
                SmsPlugin.prototype.send(number, message, 'INTENT',
                    function () { 
                       alert('Message sent successfully');  
                    },
                    function (e) {
                        alert('Message Failed:' + e);
                    }
                );               
             });            
         });

HTML 部分

<div data-role="fieldcontain">
               <input name="" id="numberTxt" placeholder="Enter mobile number" value="" type="tel" data-mini="true"><br>
               <textarea name="" id="messageTxt" placeholder="Enter message" data-mini="false"></textarea>
               <br>
               <input id="btnDefaultSMS" type="submit" data-theme="e" value="Send SMS" data-mini="false">
               <input id="btnShare" type="submit" data-theme="e" value="Share" data-mini="false">
            </div>  

smsplugin.js

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

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

配置文件

<feature name="SmsPlugin">
      <param name="android-package" value="org.apache.cordova.plugin.SmsPlugin"/>
    </feature>
于 2013-11-01T06:30:59.293 回答
1

我正在从 phonegap 发送短信,使用本地短信发送器,方法是创建一个带有使用短信协议的链接的按钮,类似于bradorego对另一个问题的建议:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>
于 2014-10-03T02:41:13.920 回答