Android以编程方式发送短信

时间:2015-06-02 09:20:36

标签: android sms

我正在尝试以编程方式发送短信,但在某些情况下它无效。

如果我这样打电话 - sendSMS("+918444380502", "BAL");短信发送成功没问题,但如果我尝试这样发送 - sendSMS("+918444380502", "El teléfono no está registrado o ligado a un usuario, favor de registrarlo");它没有发送任何线索!!

发送短信代码如下 -

//---sends an SMS message to another device---
public void sendSMS(String phoneNumber, String message)
{
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
            stopSelf();
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

2 个答案:

答案 0 :(得分:1)

由于您无法使用unicode字符发送短信,请尝试改为使用sendMultipartTextMessage

SmsManager sms = SmsManager.getDefault();
ArrayList<String> textMsgs = sms.divideMessage(message);
sms.sendMultipartTextMessage (phoneNumber, null, textMsgs, sentPI, deliveredPI);

答案 1 :(得分:0)

由于 Mike M -

,代码对我有用
private void sendSMS(String number, String msg)
{
    SmsManager sm = SmsManager.getDefault();
    ArrayList<String> parts = sm.divideMessage(msg);

    Intent iSent = new Intent(ACTION_SMS_SENT);
    PendingIntent piSent = PendingIntent.getBroadcast(this, 0, iSent, 0);
    Intent iDel = new Intent(ACTION_SMS_DELIVERED);
    PendingIntent piDel = PendingIntent.getBroadcast(this, 0, iDel, 0);

    if (parts.size() == 1)
    {
        msg = parts.get(0);
        sm.sendTextMessage(number, null, msg, piSent, piDel);
    }
    else
    {
        ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();

        int ct = parts.size();
        for (int i = 0; i < ct; i++)
        {
            sentPis.add(i, piSent);
            delPis.add(i, piDel);
        }

        sm.sendMultipartTextMessage(number, null, parts, sentPis, delPis);
        stopSelf();
    }
}

public class SmsReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equals(ACTION_SMS_SENT))
        {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        else if (action.equals(ACTION_SMS_DELIVERED))
        {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}