Recent

Author Topic: LAMW Send SMS improvement suggestion  (Read 1680 times)

CC

  • Full Member
  • ***
  • Posts: 149
LAMW Send SMS improvement suggestion
« on: March 23, 2019, 10:59:06 pm »
Hi,

the time required to finish sending a multipart message may vary quite a lot. (Up to several minutes.)
When sending multiply messages it is kind of important to follow the process and not overload the system.  It is best not to send the next SMS until the previous one is finished.  Since  jBroadcastReceiver.Receiver fires for every part sent/delivered (depending on the packageDeliveredAction parameter) we need some additional information to decide which part is the last one.  If we new the number of parts in advance it would be quite easy, all we need to do is to increase a counter every time jBroadcastReceiver.Receiver is executed and  compare it to the  the number of parts.   

Something like this:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jBroadcastReceiver2Receiver(Sender: TObject; intent: jObject);
  2. begin
  3.    Inc(Parts_Sent_Count);
  4.    if (jBroadcastReceiver(Sender).GetResultCode() <> RESULT_OK) then
  5.    begin
  6.       Handle_Failed_Message;
  7.    end
  8.    else if Parts_Sent_Count = Parts_Count then
  9.       Send_Next_Message;
  10. end;        
  11.  

But how do we get the number of parts? It is quite easy, jSend_SMS could return it to the caller. If the result is  0, something went wrong, if it is grater than zero it is the number of parts:

Code: Java  [Select][+][-]
  1. public int jSend_SMS(String phoneNumber, String msg, String packageDeliveredAction, boolean multipartMessage) {
  2.         String SMS_DELIVERED = packageDeliveredAction;
  3.         PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this.GetContext(), 0, new Intent(SMS_DELIVERED), 0);
  4.         SmsManager sms = SmsManager.getDefault();
  5.         Integer partsCount = 1;
  6.         try {
  7.            if (multipartMessage)
  8.              {
  9.                 ArrayList<String> messages = sms.divideMessage(msg);
  10.                 partsCount = messages.size();                  
  11.                 ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
  12.                 for (int i = 0; i < messages.size(); i++)
  13.                   {
  14.                         deliveredPendingIntents.add(i, deliveredPendingIntent);
  15.                   }                    
  16.                 sms.sendMultipartTextMessage(phoneNumber, null, messages, deliveredPendingIntents, null  );                      
  17.             }
  18.             else
  19.             {
  20.                  List<String> messages = sms.divideMessage(msg);    
  21.                  partsCount = messages.size();                 
  22.                  for (String message : messages)
  23.                    {
  24.                       sms.sendTextMessage(phoneNumber, null, message, deliveredPendingIntent, null );
  25.                    }                       
  26.              } 
  27.             return partsCount ;      
  28.         } catch (Exception e) {
  29.                 return 0; //fail
  30.         }
  31. }

This modification would not break any exisiting code.
« Last Edit: March 23, 2019, 11:19:22 pm by CC »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: LAMW Send SMS improvement suggestion
« Reply #1 on: March 24, 2019, 03:46:09 am »

Done!!!

Thank you!!!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

CC

  • Full Member
  • ***
  • Posts: 149
Re: LAMW Send SMS improvement suggestion
« Reply #2 on: March 28, 2019, 07:51:22 am »
Thanks! :)

 

TinyPortal © 2005-2018