Question

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 exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime(20510): FATAL EXCEPTION: main
E/AndroidRuntime(20510): java.lang.NullPointerException
E/AndroidRuntime(20510):  at android.os.Parcel.readException(Parcel.java:1328)
E/AndroidRuntime(20510):  at android.os.Parcel.readException(Parcel.java:1276)
E/AndroidRuntime(20510):  at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
E/AndroidRuntime(20510):  at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
E/AndroidRuntime(20510):  at com.example.sendsmsdemo.MainActivity.sendSMS(MainActivity.java:89)
E/AndroidRuntime(20510):  at com.example.sendsmsdemo.MainActivity$1.onClick(MainActivity.java:66)
E/AndroidRuntime(20510):  at android.view.View.performClick(View.java:2485)
E/AndroidRuntime(20510):  at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime(20510):  at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(20510):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(20510):  at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(20510):  at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(20510):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(20510):  at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(20510):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(20510):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(20510):  at dalvik.system.NativeStart.main(Native Method)

Application code:

package com.example.sendsmsdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
   Button sendBtn;
   EditText txtphoneNo;
   EditText txtMessage;
   BroadcastReceiver sendBroadcastReceiver = new sentReceiver();
   BroadcastReceiver deliveryBroadcastReciever = new deliverReceiver();;


   @Override
   protected void onPause() {
       super.onPause();

       try {
           unregisterReceiver(sendBroadcastReceiver);
           unregisterReceiver(deliveryBroadcastReciever);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
@Override
protected void onDestroy() {
   super.onDestroy();
   try {
       unregisterReceiver(sendBroadcastReceiver);
       unregisterReceiver(deliveryBroadcastReciever);
   } catch (Exception e) {
       e.printStackTrace();
   }
}

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      sendBtn = (Button) findViewById(R.id.btnSendSMS);
      txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
      txtMessage = (EditText) findViewById(R.id.editTextSMS);

      sendBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {

             if (txtphoneNo.getText().toString().equals("")
                     | txtphoneNo.getText().toString().equals(null)) {
                 Toast.makeText(getApplicationContext(), "enter_ph_no", Toast.LENGTH_LONG)
                         .show();
             } else {

                 String msg1 = txtphoneNo.getText().toString();
                 String phn1 = txtMessage.getText().toString();
                 sendSMS(msg1, phn1);

             }

            }
        });

   }
   protected void sendSMS(String phoneNumber, String message) {

      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);

      registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

      registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
      SmsManager sms = SmsManager.getDefault();
      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);


  }

   class deliverReceiver extends BroadcastReceiver {
       @Override
       public void onReceive(Context context, Intent arg1) {
           switch (getResultCode()) {
           case Activity.RESULT_OK:
               Toast.makeText(getApplicationContext(), "sms_delivered",
                       Toast.LENGTH_SHORT).show();
               break;
           case Activity.RESULT_CANCELED:
               Toast.makeText(getBaseContext(), "sms_not_delivered",
                       Toast.LENGTH_SHORT).show();
               break;
           }

       }
   }

   class sentReceiver extends BroadcastReceiver {
    @Override
       public void onReceive(Context context, Intent arg1) {

           switch (getResultCode()) {
           case Activity.RESULT_OK:
               finish();
               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;
           }           
       }
   }
}

If somebody will find some time and help me I will be really grateful

Update:

I used your code and it works:) Thanks a lot:)

private void sendSMS(String number, String message)
   {
       SmsManager sm = SmsManager.getDefault();        
       ArrayList<String> parts = sm.divideMessage(message);

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

       PendingIntent piSent = PendingIntent.getBroadcast(this, 0, new Intent(
               SENT), 0);
       PendingIntent piDel = PendingIntent.getBroadcast(this, 0,
               new Intent(DELIVERED), 0);

       registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

       registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));

       if (parts.size() == 1)
       {
           String msg = parts.get(0);
           sm.sendTextMessage(number, null, msg, piSent, piDel);
       }
       else
       {
           ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();      
           ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();       

           int ct = parts.size();
           for (int i = 0; i < ct; i++)
           {
               sentPis.add(i, piSent);
               delPis.add(i, piDel);
           }

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

I have one more question. Part of my code:

switch (getResultCode()) {
   case Activity.RESULT_OK:
   Toast.makeText(getBaseContext(), "sms_sent", Toast.LENGTH_SHORT).show();
   break;

When I sent long message (for example lenght like four single messages) I see toast "sms_sent" four times. Is it possible to see this toast only one time after successful sending the entire message?

Was it helpful?

Solution

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:

private static final String ACTION_SMS_SENT = "SMS_SENT";
private static final String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
private static final String EXTRA_MESSAGE_PART = "msg_part";

private int sendSms(String number, String message) {
    Intent iSent, iDel;
    PendingIntent piSent, piDel;

    SmsManager sm = SmsManager.getDefault();        
    ArrayList<String> parts = sm.divideMessage(message);
    final int count = parts.size();

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

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

        iDel = new Intent(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;
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top