sendSMS()方法无法读取String

时间:2014-10-06 23:45:03

标签: android android-fragments

我知道有一些明显的错误,但我尝试了很多东西,并且无法通过此方法读取全局字符串:

public void sendSMS(String phoneNumber) {
    SmsManager smsManager = SmsManager.getDefault();

    String message = mMessage;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(message);
    int messageCount = parts.size();

    Log.i("Message Count", "Message Count: " + messageCount);
    Log.i("Order", message);
    Toast.makeText(getBaseContext(), message,
            Toast.LENGTH_SHORT).show();

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

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

    for (int j = 0; j < messageCount; j++) {
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }

    // ---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;
            }
        }
    }, new IntentFilter(DELIVERED));
    smsManager.sendTextMessage(phoneNumber, null, message, sentPI,
            deliveredPI);
    /*
     * sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
     * deliveryIntents);
     */
}

此方法位于MainActivity中,其中包含ViewPager片段。使用以下方法收集字符串并将其连接起来:

public void getListToString(){
    StringBuilder sb = new StringBuilder();
    for(ReviewItem item : mCurrentReviewItems){
        sb.append(item.getTitle() + ": " + item.getDisplayValue() + "\n");
    }

    String msg = sb.toString();
    ((MainActivity)getActivity()).setMessage(msg);              
}

可以肯定的是 - 我已经用mMessage和Toasts组成了Logs。显示每个项目,但sendSMS()不想发送它。虽然,在String消息而不是mMessage中,我把&#34;一些字符串放在这里&#34;它完美地运作。任何人都可以捕获使sendTextMessage无法访问公共String mMessage的内容吗?

1 个答案:

答案 0 :(得分:1)

您的问题似乎是您的字符串超过单条短信的最大字符长度,需要拆分并与sendMultipartTextMessage()一起发送。

此最大长度取决于所用字母的字符位大小:对于标准GSM 7位字母,它是160个字符;对于8位字母表,它是140个字符;对于16位字母表(包括任何非拉丁字符的Unicode字符),它只有70。

如果您尝试发送的邮件超出了长度限制,sendTextMessage()将无提示失败,不会抛出任何错误或异常,并且不会将尝试写入提供程序(如果适用)。