SMS receive problem

drabnojdan

Member
Licensed User
Longtime User
I try to make an application to listen if a SMS arrives from a predetermined number and to display a MsgBox if the SMS arrives. The application is compiled well but when a SMS comes the MsgBox does not appear ... please help :sign0085:

this is my code:


Sub Process_Globals

End Sub

Sub Globals

Dim Phone1 As Phone
Dim PhoneSms1 As PhoneSms
Dim SmsInterceptor1 As SmsInterceptor
Dim SmsMessages1 As SmsMessages
Dim MessageReceived1 As String
Dim Msgbox1 As Notification
Dim TYPE_INBOX As Int

End Sub

Sub Activity_Create(FirstTime As Boolean)
SmsInterceptor1.Initialize(MessageReceived1)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub MessageReceived1_MessageReceived ("15555215556", "")
Msgbox("text body to show", "title")

End Sub



 
Last edited:

fdx12345

Active Member
Licensed User
Longtime User
SmsInterceptor works and here is a summary.

All of this took a lot of digging and great help from some nice people, so here is a summary to hopefully make someone elses life a little bit easier.

The examples using Si_MessageRecieved on this board do not always work. There may be things going on in each phone which could interfer. To get around this, basically modify the manifest file slightly and follow the rest of the attached code. I have tried to make normal text black and clearifications/instructions red in color. Please delete all instructions from the code or you will get compile errors.

We will be dealing wtih three areas. Code module, service module, and manifest file.

First see what you main code module is named. By Default it is called Main. If you are not sure, look at the top right of the IDE under the moduels tab. Rember the name of the code module for later use.

Let's start with the code module .. in my case it is called Main. Please excuse if there are extra stuff which do not seem to do anything as I had to do a lot of cutting from my actual probject just to keep the sample down to a bare minimum.

Here is a sample code for the code mo

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim sms1 As PhoneSms

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 Button1 As Button

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("main")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
StartService(SmsTexting)   ‘Makeup a name for the required ‘service.  I called mine SmsTexting
   ' here you send the message
   sms1.Send("xxxxx", "message to be sent")       
   ToastMessageShow("Message sent", True)‘Pop-up message stating the message was sent
   Log("SMS Sent")   ‘Log the words “SMS Sent to the Log (can read log from IDE)”.

End Sub

Sub MessageResults(fromx As String, bodyx As String)
   Dim cnt As Int
   Dim txtlen As Int
   Dim incomex As Double
   Dim MessageFrom1 As String
   Dim MessageBody1 As String
   Dim incomestring As String
   Dim acctName As String
   
   MessageFrom1 = fromx
   MessageBody1 = bodyx
   
   acctName = MessageBody1.SubString2(0,5)
   
   If acctName = "account of interest" Then
      txtlen=MessageBody1.Length
      cnt=MessageBody1.IndexOf("$")
      incomestring=MessageBody1.SubString(cnt+1)
      cnt=MessageBody1.IndexOf(",")
      incomestring = incomestring.Replace(",","") 
      
      If IsNumber(incomestring) = True Then   
         incomex = incomestring   
      Else   
         Msgbox("Not a number", "ERROR")
      End If
      
      incomex = incomestring
      
      Msgbox("From:   " & fromx & CRLF & CRLF & "$" & incomestring, "Address and Amount")
   Else
      Msgbox("From:     " & fromx & CRLF & CRLF & "Message: " & CRLF & CRLF & bodyx, "Message Results")
   End If
   
   
End Sub


‘**** Now create the Service Routine ******************
‘see below after the code section.


Creating a Service Module
• Choose Project -->add new module --> Service Module
• a pop-up window will appear asking for the service module name --> give it the name you made up above – in my case it was SmsTexting
• Next copy/paste the following:


‘** Note: the Service_Start will run each time you get a sms message



B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim si As SmsInterceptor
   
   Dim MessageFrom1 As String
   Dim MessageBody1 As String
    Type Message (Address As String, Body As String)
   
End Sub
Sub Service_Create
   si.Initialize2("si",999)

End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then        
      Dim messages() As Message   
      
messages = ParseSmsIntent(StartingIntent) ‘*this routine breaks the message into sender ‘(who the message is from” and content (body of message)


      CallSub3(Main, "MessageResults", messages(0).Address, messages(0).Body) ‘*** Main:  this routine passes the from and body of the message back to the code module, specifically the module called “Main”.  Look at your Modules tab in the IDE and see what the name is for your code module.  By Default, it is Main.  If it is not main, then replace the word “Main” with whatever you named your main module.  MessageResults:  the name of the sub to pass call in Main and pass parameters messages(0).Address, messages(0).Body.  Last messages(0).Address, messages(0).Body: parameters being passed to the Main module for futher processing.  

   End If
End Sub

Sub Service_Destroy

End Sub

Sub ParseSmsIntent (In As Intent) As Message()    
    Dim messages() As Message    
   If In.HasExtra("pdus") = False Then Return messages    
   Dim pdus() As Object    
   Dim r As Reflector    
   pdus = In.GetExtra("pdus")    
   If pdus.Length > 0 Then        
      Dim messages(pdus.Length) As Message        
      For i = 0 To pdus.Length - 1            
         r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _                
         Array As Object(pdus(i)), Array As String("[B"))            
         messages(i).Body = r.RunMethod("getMessageBody")            
         messages(i).Address = r.RunMethod("getOriginatingAddress")        
      Next    
   End If    
   
   Return messages
End Sub

The CallSub3 is a neat command to call a subroutine in another module - in this case the "Main" module.

The last thing to do is add something to the Manifest file. You get to the manifest file through Project --> Manifest Editor

once inside the manifest editor add the following at the end of the file:

B4X:
AddPermission(android.permission.RECEIVE_SMS)
AddReceiverText(SmsTexting, <intent-filter>    <action android:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter>)


***** note SmsTexting in the AddreceiverText is the name of my service module.


Now you are done and it should work fine.
 
Upvote 0
Top