0

I just found out my app doesn't capable of sending more than 160 characters at a time. it works fine on characters less than 160. when I try to send more than 160 characters at a time. it display "messages sent" toast but message doesn't going anywhere what should I change to send more than 160 characters. thank you
here is the code

public class MainActivity7 extends ActionBarActivity {
String value ;
    Button button;
    TextView editext2;
    TextView editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity7);

       editText = (TextView) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        editext2 = (TextView) findViewById(R.id.editText2);

        Intent a = getIntent();

      editText.setText(a.getStringExtra("item")  );


       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               sendSMSMessage();
           }


       });



    }

    private void sendSMSMessage() {
        Log.i("Send SMS", "");
        String phoneno = editext2.getText().toString();
        String message = editText.getText().toString();
      try{
          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage(phoneno,null,message,null,null);
          Toast.makeText(getApplicationContext(),"sms sent.",
                  Toast.LENGTH_LONG).show(); }
      catch (Exception e){
          Toast.makeText(getApplicationContext(),
                  "sms failed,please try again",
                  Toast.LENGTH_LONG).show();
          e.printStackTrace();
      }



    }
}
APP Bird
  • 1,371
  • 1
  • 20
  • 37

1 Answers1

1

To send more than 160 characters in SMS you need to send it as a multiple SMS.

SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(LONG_TEXT);
int numParts = parts.size();

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

for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(getContext(), 0, mSendIntent, 0));
deliveryIntents.add(PendingIntent.getBroadcast(getContext(), 0, mDeliveryIntent, 0));
}

sm.sendMultiPartTextMessage(mDestAddr,null, parts, sentIntents, deliveryIntents);
Shvet
  • 1,159
  • 1
  • 18
  • 30