Android send SMS function code

  • 2020-06-07 05:15:41
  • OfStack

This article describes Android sending SMS code, Android program development is a very common important function. To share with you for your reference. The specific methods are as follows:


//  Send a text message 
public void sendMsg(){
  String content = edtSend.getText().toString();
  SmsManager smsManager = SmsManager.getDefault();
  List<String> divideContents = smsManager.divideMessage(content);
  for (String text : divideContents) {  
    smsManager.sendTextMessage(smsWidget.str_number, null, text, null, null);  
  } 
}

The above code first obtains an instance of SmsManager; Then, divideMessage() is used to divide the message content (content) into several parts. This is because if the message content is too long, it may exceed the maximum length allowed. In this case, the message content should be divided into several strings. Finally, for all strings, send using the sendTextMessage() method, where smsWidget.str_number is the counterpart number and text is every 1 string.

I hope this article has been helpful for your Android programming.


Related articles: