如何使用SmsManager发送很长的消息

use*_*255 2 android smsmanager

基本上我有一个聊天应用程序,我用SmsManager在客户端之间发送短信,但我能够发送比它更长的消息.我读到它只能发送大约40-60个字母,但我想发送200-400个音符的消息所以有没有办法用它来做呢?

Aja*_*jay 5

我为你创建了一个演示..我想它可能对你有帮助..

它会将你的信息分成几部分并逐一发送......

public class Home extends Activity
{
    Context ctx;

    private static final String SMS_SEND_ACTION = "CTS_SMS_SEND_ACTION";
    private static final String SMS_DELIVERY_ACTION = "CTS_SMS_DELIVERY_ACTION";

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

    String ph_no;
    String str;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        ctx = this;

        ph_no = "set destination mob. no here..";
        str = "Your message here....";

        SmsManager sm = SmsManager.getDefault();

        IntentFilter sendIntentFilter = new IntentFilter(SMS_SEND_ACTION);
        IntentFilter receiveIntentFilter = new IntentFilter(SMS_DELIVERY_ACTION);

        PendingIntent sentPI = PendingIntent.getBroadcast(ctx, 0,new Intent(SMS_SEND_ACTION), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(ctx, 0,new Intent(SMS_DELIVERY_ACTION), 0);

        BroadcastReceiver messageSentReceiver = new BroadcastReceiver()
        {      
            @Override
            public void onReceive(Context context, Intent intent)
            {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        registerReceiver(messageSentReceiver, sendIntentFilter);

        BroadcastReceiver messageReceiveReceiver = 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;                        
                }
            }
        };

        registerReceiver(messageReceiveReceiver, receiveIntentFilter);

        ArrayList<String> parts =sm.divideMessage(str);

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

        for (int i = 0; i < parts.size(); i++)
        {
            sentIntents.add(PendingIntent.getBroadcast(ctx, 0, new Intent(SMS_SEND_ACTION), 0));
            deliveryIntents.add(PendingIntent.getBroadcast(ctx, 0, new Intent(SMS_DELIVERY_ACTION), 0));
        }

        sm.sendMultipartTextMessage(ph_no,null, parts, sentIntents, deliveryIntents);
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码还将为您提供发送和发送通知.(此发送和发送通知是附加代码,但在某处它是完全使用的)