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:

melamoud

Active Member
Licensed User
Longtime User
yes

lets say you have a Dim define in your Main activity, and you want to access it from your service, all you need to do is use Main.varName
the same works for Dim in your Service that you want to access from the activity

if you want to call a Sub, you need to use CallSub method - but in that case the activity need to be active.
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Here is what I have:

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
   Dim si As SmsInterceptor
   Dim pe As PhoneEvents
   
   Dim MessageFrom1 As String
   Dim MessageBody1 As String
   
End Sub

The problem is the si_MessageRecieved is never called.  What do I need to do to make it work?

Bill


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
   Dim 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("main")
   si.Initialize2("MessageRecieved",999)
   StartService(callsms)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
   'Msgbox("This is a msgbox test","A test Message-Title")
   ' here you send the message
   EditText1.Text="xxxxxx"      'note:  hid number
   sms1.Send(EditText1.Text, "bal")
   ' show a Toast Message
   ToastMessageShow("Message sent", True)
   ' and write log
   Log("SMS Sent")
End Sub

Sub si_MessageRecieved (From As String, Body As String) As Boolean
   MessageFrom1 = From
   MessageBody1 = Body
   ToastMessageShow(MessageBody1, True)
   Msgbox("From " & From & " Text " & Body, "Title")
End Sub
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
What do I need to do to have the MessageRecieved function work?

From above, what do I need to add and were?

Thanks much,

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
MessageRecieve not working

I have figured out all of what was going on in the eariler part of this thread and have created a service module identical to the example on page 1 of this thread. I am calling that serice module from the code module. I have stepped through the code and the call is working just fine. However, the SI_MessageRecieve is not used. On top of that, I can use line breaks from the start of the initial code, through the service module and then the line breaks stops working. All of the line breaks I have after the StartService command do not work. The code continues right on through as if the line break was not there.

I am using the latest versioin of B4a IDE and the new Note2 Samsung phone.
Any ideas?

Bill
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
you need (maybe you did that) to move the si.Initialize2("MessageRecieved",999) and the SI_MessageRecieve to the service - cause the activity might be paused when the SMS arrive.

if this does not work, I can show you a different way to do this instead of using SmsInterceptor and phone events use the manifest file to regsiter to the SMS event - much better works in more cases - here is the thread

http://www.b4x.com/forum/basic4andr...pting-sms-messages-background.html#post116193
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Here is What I have

Here is the code and the service sections. I have seen the MessageRecieved used as initialize("SI") then sub SI_MessageRecieved and also seen it declared initialize("Message", 999) with sub SI_MessageRecieved. I do not know which is correct but neither works.

My code section:
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
   Dim EditText1 As EditText
End Sub

Sub Service_Create


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)
   ' here you send the message
   EditText1.Text="xxxxx"      'note:  hid number
   sms1.Send(EditText1.Text, "bal")
   ' show a Toast Message
   ToastMessageShow("Message sent", True)
   ' and write log
   Log("SMS Sent")

End Sub

[B][U]Now my Service Section:[/U][/B]

'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

End Sub
Sub Service_Create
   si.Initialize2("MessageRecieved",999)

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub


Sub si_MessageRecieved (From As String, Body As String) As Boolean
   MessageFrom1 = From
   MessageBody1 = Body
   ToastMessageShow(MessageBody1, True)
End Sub


Note:  I named my service module SmsTexting.

The si_MessageRecieved sub never runs.

Bill
 
Last edited:
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
I think you should change this line si.Initialize2("MessageRecieved",999) to be si.Initialize2("si",999)

if thats not working, you can try the method I suggested - using the manifest file, if you want a code example I can provide it, but it exists in the thread link I published in me previous answer
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Not working

I tried the si.Initialized2("si",999) with a sub si_MessageRecieved and a sub si_si and neither was called when the text message replay arrived.

When I recieved the reply text message, the Note2 Phone displays the message as a single line at the top of the B4a-bridge application. It is not my code doing the display ... ie not a ToastMessageShow. I have a breakpoint set inside the MessageRecieved and it never hits.

I would love for this to work but if the phone itself is interfering (os app grabbing the message first) then I will need to find a different way of handling this problem. It needs to be a way that works on multiple phones.


Bill
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
have you tried the manifest / broadcast / option I suggested ?

it is working for me, I always prefer to use intents Vs other options, its more robust in my mind.
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
I do not know how to do that option. Took me awhile to figure out this approach. Do you have a working sample code and instructions?

Thanks much,

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Where is the Manifest file?

I am using Basic4Android IDE and am not seeing a manifest file or a pull down option to create one. Under Project --> add new module, my choices are activity, class code, and service.

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Almost there

I have everything working in the service module. Using the manifest approach, I can get to the "from" and "body" via:

messages(0).Address and
messages(0).Body

I need to pass these two bits of data back to the code module as the service module times out very quickly and kills itself along with whatever one was doing in that section.

How can I pass this information back to the code module as in a sub call such as HereAreMyResults(messages(0).Address, messages(0).Body) or some other way if that other way is simpler.

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
It works!!!!

Thank you! Later today or tomorrow, I will put a detail discription and code on here for other people so they do have to go and do all of the diggging and asking for help like I did.

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Line Feed

One more thing. I am using a msgbox is temporarly display results - which is working just fine but I need to insert a line feed/new line in the Msgbox string. I know how to do this in regular VB but Basic3Android does not understand those commands. Does anyone know how to do it inside of a

B4X:
Example:
Msgbox?  --> Msgbox ("some stuff" & newlineCommmand & "Someother stuff", "My stuff to display").

Thanks in advance.

Bill
 
Upvote 0

fdx12345

Active Member
Licensed User
Longtime User
Stop Service

I my app, I only need to run the service once then kill it until user decides to run it again via a pushbutton. How do I kill the service so I am not doing a startservice on a service that is already running?

Bill
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
I think it just stop running when it end of the Service_Start sub,
the process is not going to be destroyed, but you do not need that anyway right (service is part of the same process as the activity I think)

then you can call start again,
I think BTW that if you call start when the process is in the middle of running, nothing happen, but I'm not sure about that.
 
Upvote 0
Top