android以编程方式从后付费服务发送短信

时间:2014-11-14 04:34:58

标签: android sms smsmanager

我正在使用代码

以编程方式发送短信
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);

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

但如果手机有后付费网络连接,则不会发送消息。如何处理?

1 个答案:

答案 0 :(得分:0)

我的应用程序遇到了同样的问题。但是,我发现如果消息计数超过160,则sms.sendTextMessage()无法正常工作。即。您必须将消息分成几部分,然后分成sms.sendMultiPartTextMessage()。 只有英文字符<= 160才能发送短信。如果您使用任何特殊字符,则计数(长度)应为&lt; = 70。以下是我的代码。我正在使用阿拉伯字符。

private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

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

    if (message.length() <= 70) {
        SmsManager sms = SmsManager.getDefault();
        Log.d(LOG_TAG, "Send Sms : " + message + " serverNumber "
                + phoneNumber);
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    } else {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> parts = sms.divideMessage(message);
        int numParts = parts.size();

        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

        for (int i = 0; i < numParts; i++) {
            sentIntents.add(PendingIntent.getBroadcast(act, 0, new Intent(
                    SENT), 0));
            deliveryIntents.add(PendingIntent.getBroadcast(act, 0,
                    new Intent(DELIVERED), 0));
        }
        Log.d(LOG_TAG, "Send Sms multipart : " + message + " serverNumber "
                + phoneNumber);
        sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
                deliveryIntents);
    }
}

我希望这会有所帮助。