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:

drabnojdan

Member
Licensed User
Longtime User
I tried that as service but I have no succeeded in this way. The application is compiled well but when a SMS comes the MsgBox does not appear ... Where can I find an example to help me? Or a full documentation? I read on the forum, tutorials and documentation on the site and have not found.


'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 SmsInterceptor1 As SmsInterceptor
Dim SmsMessages1 As SmsMessages

End Sub

Sub Service_Create
SmsInterceptor1.Initialize(MessageReceived1)
End Sub

Sub Service_Start
SmsInterceptor1.Initialize(MessageReceived1)
End Sub

Sub Service_Destroy

End Sub
 
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   '
dim MessageFrom1 as string
dim MessageBody1 as string
dim SI as smsinterceptor
   
End Sub
Sub Service_Create
    SI.Initialize("SI")
   

   
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
        toastmessageshow(MessageFrom1, true)

end sub

Note that you should start the service with StartService in another activity.
and note that msgbox doesn't work in a service.

XverhelstX
 
Upvote 0

Omar

Member
Licensed User
Longtime User
Hello Friends,

I am also working on a similar project that sends out an sms and then waits to receive back two sms messages.

I just cannot understand how to make the program wait to receive the message and extract the information from the messages.

I have gone over this thread and also reviewed the Services tutorial but just cannot understand this still. I am still a :sign0104: and will really appreciate your help.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
Hey Omar,

A quick example with explanations:

First create an activity and add a button to it.
In the activity add the following code:
B4X:
Sub btnService_Click
startService(Interceptor) 'Interceptor can be changed to the service you name.
End Sub

What this does is just start the service Interceptor.

Next make the Service (service works in the background, not like an activity.) and call it Interceptor or anything you want to intercept the incoming message. Then add the following code in the service:


B4X:
'Service module called Interceptor
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   '
dim MessageFrom1 as string
dim MessageBody1 as string
dim SI as smsinterceptor
   
End Sub
Sub Service_Create
    SI.Initialize("SI")
   

   
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
        toastmessageshow(MessageFrom1, true)

end sub

Everytime (when your service is running ofcourse) it will call this snippet of code:

B4X:
Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
        toastmessageshow(MessageFrom1, true)

end sub

This is the part where you received the message with From as the phone number from who it came and Body as the message itself.
Toastmessageshow shows a "'blue popup message" with the phone number in it. We use toastmessageshow because Msgbox doesn't work in services.
I declare MessageFrom1 and MessageBody1 as Process Globals strings because you can use them in other activities then.
for example a label in your other activity could be:

B4X:
dim lblphone, lblbody as label
lblphone.text = interceptor.MessageFrom1
lblbody.text = interceptor.MessageBody1

Hope I helped U ;)

If you have any more questions, feel free to ask.

Tomas
 
Last edited:
Upvote 0

Omar

Member
Licensed User
Longtime User
Thank you very much Tomas.

I truly appreciate your very detailed explanation and I will try to work with this today and hopefully will make it work.

Again thank you for your kind help.

Omar


Hey Omar,

A quick example with explanations:

First create an activity and add a button to it.
In the activity add the following code:
B4X:
Sub btnService_Click
startService(Interceptor) 'Interceptor can be changed to the service you name.
End Sub

What this does is just start the service Interceptor.

Next make the Service (service works in the background, not like an activity.) and call it Interceptor or anything you want to intercept the incoming message. Then add the following code in the service:


B4X:
'Service module called Interceptor
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   '
dim MessageFrom1 as string
dim MessageBody1 as string
dim SI as smsinterceptor
   
End Sub
Sub Service_Create
    SI.Initialize("SI")
   

   
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
        toastmessageshow(MessageFrom1, true)

end sub

Everytime (when your service is running ofcourse) it will call this snippet of code:

B4X:
Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
        toastmessageshow(MessageFrom1, true)

end sub

This is the part where you received the message with From as the phone number from who it came and Body as the message itself.
Toastmessageshow shows a "'blue popup message" with the phone number in it. We use toastmessageshow because Msgbox doesn't work in services.
I declare MessageFrom1 and MessageBody1 as Process Globals strings because you can use them in other activities then.
for example a label in your other activity could be:

B4X:
dim lblphone, lblbody as label
lblphone.text = interceptor.MessageFrom1
lblbody.text = interceptor.MessageBody1

Hope I helped U ;)

If you have any more questions, feel free to ask.

Tomas
 
Upvote 0

Omar

Member
Licensed User
Longtime User
Tomas,

I just wanted to thank you for your kind help, with your detailed explanation I have managed to understand the basic concept of the Services and also to some extent implement the working which I was trying.

I would also like to ask you do you know if it is possible to transfer data from the service to the main activity, for example if I declare the variable Data in the main activity and then save some string into it from the service but this does not display in my application for some reason.

Thank you once again.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
As you can see, we declare MessageFrom1 and MessageBody1 as Process_globals.
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   '
dim MessageFrom1 as string
dim MessageBody1 as string
dim SI as smsinterceptor
   
End Sub

When you declare under Process_Globals, you can get those variables from another activity/Service/Code module

To get the string in your main activity, we first need to insert the data(phone number and message body into the string. What we do as follow:
B4X:
Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From 'here we give the Process_global variable the value From. (From As String)
   MessageBody1 = Body 'here we give the Process_global variable the value Body. (Body As String)
        toastmessageshow(MessageFrom1, true)
        callsub("Main","MessageReceived") ' I will explain this later.


end sub

Now in your main activity, you can just normally declare two strings (doesn't have to be Process now because you will only need it in the activity itself) that we now call string strPhoneNumber and strPhoneBody. so:
B4X:
dim strPhoneNumber, strPhoneBody as string

Now you could add something like the following code in the activity:
It's best that you type this code instead of copying as you will understand it.

B4X:
Sub MessageReceived
strPhoneNumber = interceptor.MessageFrom1
strPhoneBody = interceptor.MessageBody1
End Sub

If you have typed this code, you can see that when you typed the dot (.) after interceptor it automatically gives you 2 possiblities. (interceptor.)
These possibilities are the process_globals, indeed ;)
Ok, now I explain the callsub("Main","MessageReceived")
When you call callsub, Callsub calls a sub in an activity, so here in activity Main, we call MessageReceived.
In 'MessageReceived', we update the strings strPhoneNumber and strPhoneBody with the values of the interceptors.

Now you have the values stored in your main activity.

Now you can add a button that shows the value.

B4X:
sub btnShow_Click
msgbox(strPhoneBody, strPhoneNumber)
end sub

This will show a message box with the text of strPhoneBody as message and the caption is the phone number.

I hope the codes works a bit cause i made it a bit from scratch

Cheers, ;D

XverhelstX
 
Last edited:
Upvote 0

Omar

Member
Licensed User
Longtime User
Dear Tomas,

I can't thank you enough for your assistance and patience. I am extremely appreciative of your very detailed overview and explanation and has made understanding this process so much easier.

Thank you once again.


Omar
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Thank you very much Tomas.

I truly appreciate your very detailed explanation and I will try to work with this today and hopefully will make it work.

Again thank you for your kind help.

Omar

I tried this code above but my app get error on the the device aplcation sopped unexpected when the message resive do any one have the same problem?
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Can we see your code?

Here it is can anyone tell me what i do wrong the app crash evrytime i get an message.

Main
B4X:
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 strPhoneNumber, strPhoneBody As String
   Dim Button1 As Button
   Dim EditText1 As EditText
    Dim bnr As String
   
   End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
StartService(callsms)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub MessageReceived
   strPhoneNumber = callsms.MessageFrom1
   strPhoneBody =   callsms.MessageBody1    
End Sub

Sub btnShow_Click
End Sub

Sub Button2_Click
   Msgbox(strPhoneBody,strPhoneNumber)
End Sub

sevice callsms

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    '
Dim MessageFrom1 As String
Dim MessageBody1 As String
Dim SI As SmsInterceptor
    
End Sub
Sub Service_Create
    SI.Initialize("SI")
      
End Sub

Sub Service_Start

End Sub

Sub SI_MessageReceived (From As String, Body As String)
    MessageFrom1 = From 'here we give the Process_global variable the value From. (From As String)
    MessageBody1 = Body 'here we give the Process_global variable the value Body. (Body As String)
    ToastMessageShow(MessageFrom1, True)
     CallSub("Main","MessageReceived") ' I will explain this later.


End Sub
 
Last edited:
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
There is nothing wrong whith my code. I add phone 1.36 instead off 1.75 than it works. but then my otther app with vibrate dont work is there anny other phone lib?
 
Last edited:
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
StartService problem

I have everything down but my SI_MessageRecieved sub is not running. I have tired to add the start service but get an undeclared variable when I use StartService(Interceptor).

If I declare Dim SI as SmsInterceptor then do a StartService(SI), the program compiles with no errors but crashes when run on the simulator or on my phone.

How do I start the service so SI_MessageRecieved will be excecuted?

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Service Name

That is the problme I am having. Is the service name Interceptor, SmsInterceptor, SI or something else?

I thought there was a service tutorial but I am unable to find it. In the meantime, what is the service name to use in order for SI_MessageRecieved to work?

Thanks much,

bill
 
Upvote 0
Top