Android Question SMS sending: complete procedure

Discussion in 'Android Questions' started by Kibe, Sep 2, 2015.

  1. Kibe

    Kibe New Member

    Hi. Am quite new to B4a. What is the complete procedure of sending sms? I have come across pieces of information till I got confused.
    Thanks
     
  2. Derek Johnson

    Derek Johnson Active Member Licensed User

    Add the Phone library then:

    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        'Activity.LoadLayout("Layout1")
        Dim PhoneNumber as String ="07700912345" 'UK format - un-issued number"
        Dim SMS As PhoneSms
        
    Dim Message As String = "Hi there"
        
    SMS.Send(PhoneNumber,Message)
    End Sub
     
    Erel likes this.
  3. Kibe

    Kibe New Member

    Thanks Derek.
    I have tried using Kenyan format so far no success. Just copied your code the way it is. Am I supposed to do more that adding Phone library? There must be a way for the code to tell me whether the sms is sent and delivered.
    Thanks
     
  4. Derek Johnson

    Derek Johnson Active Member Licensed User

    I presume that the device you are testing on has credit, and can send to the number that you are using in your code.

    So you have a mobile number like "02547nnxxxxxx" where 02547 is the prefix for all Kenyan numbers when calling from inside Kenya.

    There is a way to tell if the SMS has been sent from your phone, I'll post some more details on this later.
     
  5. Kibe

    Kibe New Member

    Thanks a lot Derek, I got it right now. I used +25472xxxxxxx
    If you don't mind and at your own time, can I receive sms on my app just like I have been able to send?
    Thanks.
     
    Last edited: Sep 2, 2015
  6. Derek Johnson

    Derek Johnson Active Member Licensed User

    Here is some test code that will try to send an SMS and then detect if the SMS has been sent from your phone. It won't tell you though whether the network was able to deliver it though.

    N.B Delivery notices do not work in my experience.

    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: SMSTest
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region


    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Dim SMS As PhoneSms
        
    Dim PE As PhoneEvents
        
    Dim PhoneID As PhoneId
        
    Private Button1 As Button
        
    Private Label1 As Label
        
    Private EditText1 As EditText
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        Activity.LoadLayout("Layout1")
        EditText1.Color=
    Colors.White
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
       
    End Sub

    Sub Button1_Click
        Test
    End Sub

    Sub Test
        
    Log("Sending Message")
        PE.InitializeWithPhoneState(
    "PE"PhoneID)
        
    Dim PhoneNumber As String ="07700900000" 'UK format - un-issued number!
        Dim Message As String = "Hi there at " & DateTime.Time(DateTime.Now)
        
    'SMS.Send(PhoneNumber,Message)
        Dim WantSentStatus As Boolean = True
        
    Dim WantDeliveryNotification As Boolean = True
        
    Try
            EditText1.Text=
    $"Sending Message to  ${PhoneNumber}
    Message: ${
    Message}
    "$

            
    SMS.Send2(PhoneNumber,Message,WantSentStatus,WantDeliveryNotification)
        
    Catch
            
    Log("Error when sending: " & LastException.Message)
            EditText1.Text=EditText1.Text &  
    LastException.Message & CRLF
        
    End Try   
    End Sub

    Sub PE_SmsDelivered (PhoneNumber As StringIntent As Intent)
        
    'Have never got a receipt from SMS using this!
        Dim Result As String
        Result=
    "Receipt received from " & PhoneNumber
        
    Log(Result)
        EditText1.Text=EditText1.Text & Result & 
    CRLF
    End Sub

    Sub PE_SmsSentStatus(Success As Boolean, ErrorMessage As String, PhoneNumber As StringIntent As Intent)
        
    Dim Result As String
        Result=
    "SMS Sent Status:" & Success & " : " & ErrorMessage & " " & PhoneNumber
        
    Log(Result)
        EditText1.Text=EditText1.Text & Result & 
    CRLF
    End Sub
    I've attached the zipped project.

    You can go further and get copies of outgoing and incoming SMS messages using the SMSInterceptor object. This might be what you initially found confusing.

    Derek
     

    Attached Files:

    Matias Ferrer and Johan Schoeman like this.
  7. Kibe

    Kibe New Member

    Once again am grateful. I will test this as I try to figure out how my app can receive sms. Thanks
     
  8. Derek Johnson

    Derek Johnson Active Member Licensed User

    Here's a more complete App that both sends and processes received SMS messages.

    This is considerably more complex as it requires a background process to handle the receipt of SMS messages.

    When an SMS message is received it adds a notification. Clicking on the notification (re)opens the App.

    Derek
     

    Attached Files:

    Matias Ferrer and OrdoConcept like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice