Skip to content Skip to sidebar Skip to footer

Sms Application Gives An Error When I Send Long Messege

I copy some code from internet to create sms application. It works well with short messages. When I want to send long message I get error: W/dalvikvm(20510): threadid=1: thread ex

Solution 1:

The sendMultipartTextMessage() method can be used to send both single and multipart messages. If the length of the message passed to sendTextMessage() is, indeed, causing the NullPointerException, this should work:

privatestaticfinalStringACTION_SMS_SENT="SMS_SENT";
privatestaticfinalStringACTION_SMS_DELIVERED="SMS_DELIVERED";
privatestaticfinalStringEXTRA_MESSAGE_PART="msg_part";

privateintsendSms(String number, String message) {
    Intent iSent, iDel;
    PendingIntent piSent, piDel;

    SmsManagersm= SmsManager.getDefault();        
    ArrayList<String> parts = sm.divideMessage(message);
    finalintcount= parts.size();

    ArrayList<PendingIntent> sentPis = newArrayList<>(count);      
    ArrayList<PendingIntent> delPis = newArrayList<>(count);       

    for (inti=0; i < count; i++) {
        iSent = newIntent(ACTION_SMS_SENT)
                    .putExtra(EXTRA_MESSAGE_PART, i);
        piSent = PendingIntent.getBroadcast(this,
                                            i,
                                            iSent,
                                            PendingIntent.FLAG_ONE_SHOT);
        sentPis.add(piSent);

        iDel = newIntent(ACTION_SMS_DELIVERED)
                   .putExtra(EXTRA_MESSAGE_PART, i);
        piDel = PendingIntent.getBroadcast(this,
                                           i,
                                           iDel,
                                           PendingIntent.FLAG_ONE_SHOT);
        delPis.add(piDel);
    }

    sm.sendMultipartTextMessage(number, null, parts, sentPis, delPis);

    return count;
}

Solution 2:

If your sms's length is more than 160 it will give an error as if exceed the length of one sms that the mobile can send

Post a Comment for "Sms Application Gives An Error When I Send Long Messege"