Trouble with sendMultipartText in android

筅森魡賤 提交于 2019-12-13 20:26:05

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14137836/trouble-with-sendmultiparttext-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!