使用Phonegap / Cordova 3.1发送Android短信

时间:2013-11-01 06:16:15

标签: android cordova phonegap-plugins

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

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

2 个答案:

答案 0 :(得分:1)

在这里,我分享了我的工作短信插件。请尝试这个。我希望这能帮到您。如有任何问题,请告诉我。

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

的index.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());
});

config.xml中

<feature name="SmsPlugin">
      <param name="android-package" value="org.apache.cordova.plugin.SmsPlugin"/>
    </feature>

答案 1 :(得分:1)

我从phonegap发送短信,使用本机短信发件人创建一个带有使用短信协议的链接的按钮,类似于另一个问题的bradorego's suggestion

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>