Trouble with sendMultipartText in android

2019-09-18 14:15发布

I recently ran into a little trouble with sending SMSes as sendTextMessage can only send SMSes of 160 characters or lower. I, however, wish to send a long text wishing to prompt another user to input information.

Here's my code:

//sends a SMS message to another device
private void sendSMS(String phoneNo, String text)
{     

    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;                      
            }
        }
    }, new IntentFilter(DELIVERED));



    displaySent(text, phoneNo);     

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(text);
    sms.sendMultipartTextMessage(phoneNo, null, parts, sentPI, deliveredPI);***********

    //SmsManager sms = SmsManager.getDefault();
    //sms.sendTextMessage(phoneNo, null, text, sentPI, deliveredPI);



}

At the line marked with the Asterixs, the error "The method sendMultipartTextMessage(String, String, ArrayList, ArrayList, ArrayList) in the type SmsManager is not applicable for the arguments (String, null, ArrayList, PendingIntent, PendingIntent)" appears. I hope this will help.

Any help with this would be much appreciated.

On a side note, i am wondering why can't all methods to send text messages be simply one that can break up all messages on its own already? Why the need to have sendTextMessage and sendMultipartTextMessage when i assume, that sendMultipartMessage can do the job of sendTextMessage. Any suggestions to why this may be would also be greatly appreciated.

标签: android sms
1条回答
Viruses.
2楼-- · 2019-09-18 14:56

Change your code as for sending message using sendMultipartTextMessage:

SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(text);


ArrayList<PendingIntent> sentPIarr = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPIarr = new ArrayList<PendingIntent>();

for (int i = 0; i < parts.size(); i++) {
sentPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0));
deliveredPIarr.add(PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0));
}

sms.sendMultipartTextMessage(phoneNo, null, parts, sentPIarr, deliveredPIarr); 

because sendMultipartTextMessage method take ArrayList as fourth and fifth param of pendingIntents

查看更多
登录 后发表回答