receiving an SMS in the background

RobVanBrunschot

Member
Licensed User
Longtime User
Hello,

The program in thread http://www.b4x.com/forum/questions-help-needed/1015-sms-reciever.html works okee but can I run the program without a form in the background were it waits on an incoming SMS and then continues/intercepts when one is received?

My code is similar but instead of puting the text in a label I want to show the message with for instance a MsgBox.

Like this,
Sub Intercept_MessageReceived
Sms.Value = Intercept.ReceivedMessage
Msgbox("SMS received from " & SMS.From & " at " & Time(SMS.DateReceived) & CRLF & CRLF & SMS.Body)
End Sub


When I run the program, nothing happens and I think it does not even run anymore.

Regards, Rob
 

mjcoon

Well-Known Member
Licensed User
... but can I run the program without a form in the background ...
When I run the program, nothing happens and I think it does not even run anymore.

If you do not show a form then the program closes itself when App_Start completes. It thinks it has nothing to wait for...

Do you mean that the program no longer shows up on the list of running programs?

Furthermore if you do show a form and there is an "OK" button, clicking that will also close the program unless, for instance, you use FormLib.dll and do:
B4X:
Formlib.MinimizeBox = True

This will show an "X" instead of "OK" and clicking it will hide the program but not close it so it can run "in the background". Not sure what happens about a pop-up MsgBox in that case; you may need to arrange that it moves itself to the foreground. Which may not be convenient if you are using another program...

Mike.
 

RobVanBrunschot

Member
Licensed User
Longtime User
Thanks for your reponse, mjcoon.

I tried with the Hardware1.ShowTodayScreen but it stays ontop of my device.
My code looks like this, it seems that the form is trying to go in the background but than it stays ontop.

Any ideas on how to put the form in the background so the program stays running?
Regards, Rob

B4X:
Sub Globals
   HKCUkey = "Software\RAD\SMS Viewer"
   Dummy   = "" 
End Sub

Sub App_Start
   Flb.New1("Form1",B4PObject(1))
   Flb.MinimizeBox = True
   Hardware1.New1
   SMS.New2
   Intercept.New1
   Reg.New1
   
   ' Create the registry key if not allready exists
   Reg.RootKey(reg.rtCurrentUser)
   ErrorLabel(NotCreatedYet)
   Dummy = reg.GetValue(HKCUkey,"From")
   Goto RegistryCreatedOrExisting
NotCreatedYet:
   reg.CreateSubKey("",HKCUkey)
RegistryCreatedOrExisting:

   Form1.Show
   Msgbox("Going to background...")
   Hardware1.ShowTodayScreen
End Sub

Sub Intercept_MessageReceived
   TimeFormat("HH:mm")
   Sms.Value = Intercept.ReceivedMessage
   Reg.SetStringValue(HKCUkey,"From",SMS.From)
   Reg.SetStringValue(HKCUkey,"Text",SMS.Body)
   Reg.SetStringValue(HKCUkey,"Received at",Time(SMS.DateReceived))
   Msgbox("SMS ontvangen van " & SMS.From & " op " & Time(SMS.DateReceived) & CRLF & CRLF & SMS.Body)
End Sub

Sub Form1_Close
   Msgbox("Closing...")
End Sub

Sub Form1_Show
   Msgbox("Hi...")
End Sub
 

RobVanBrunschot

Member
Licensed User
Longtime User
Hello, I have the program working.

First it shows the screen and than I make the Today screen active, making the program going to the background.
I found out, somewhere in the forum, that I need a timer of sorts to keep the program running in the background.
My worry is, does it not take to much battery when a timer is running, I am afraid that the battery will go down within a few hours.

Does anyone know a better manner to keep the program running in the background without draining the battery.

hereby my code so far, I use the registry to show the sms in combination with Wisbar Advance Desktop.

Regards, Rob

B4X:
Sub Globals
   Dim words(0) As string
   HKCUkey = "Software\RAD\SMS Viewer"
   Dummy   = "" 
End Sub

Sub App_Start
   Hardware1.New1
   SMS.New2
   Intercept.New1
   Reg.New1
   
   ' Create the registry key if not allready exists
   Reg.RootKey(reg.rtCurrentUser)
   ErrorLabel(NotCreatedYet)
   Dummy = reg.GetValue(HKCUkey,"From")
   Goto RegistryCreatedOrExisting
NotCreatedYet:
   reg.CreateSubKey("",HKCUkey)
RegistryCreatedOrExisting:

   Form1.Show
   Form1.Text = ""
   
   Timer2.Enabled = False
   Timer1.Enabled = True
   Timer1.Interval = 1000
End Sub

Sub Intercept_MessageReceived
   Dim frm,tel,txt,tijd,str,wordnr,line,linenr,linestringcount
   TimeFormat("HH:mm")
   Sms.Value = Intercept.ReceivedMessage
   frm  = SMS.From
   txt  = SMS.Body
   tijd = Time(SMS.DateReceived)

   Label1.Text = frm  : Reg.SetStringValue(HKCUkey,"From",frm)
   Label2.Text = txt  : Reg.SetStringValue(HKCUkey,"Text",txt)
   Label3.Text = tijd : Reg.SetStringValue(HKCUkey,"Received at",tijd)
   tel    = SubString(frm,StrIndexOf(frm,"<",0)+1,StrLength(frm)-StrIndexOf(frm,"<",0)-2)
   frm    = SubString(frm,1,StrLength(frm)-StrIndexOf(frm,Chr(34),1)+1)
   Reg.SetStringValue(HKCUkey,"Name",frm)
   Reg.SetStringValue(HKCUkey,"Tel",tel)
   str    = "AAA"
   wordnr = 0
   line   = ""
   linenr = 0
   linestringcount = Reg.GetValue(HKCUkey,"Line String Count")
   Do While str <> ""
      nr  = nr + 1
      str = Word(txt,nr," ","")
      If str <> "" Then
         If StrLength(line & " " & str) > linestringcount Then
            linenr = linenr + 1
            Reg.SetStringValue(HKCUkey,"Line " & linenr,line)
            line = str
         Else
            If line = "" Then
               line = str
            Else
               line = line & " " & str
            End If
         End If
      End If
   Loop
   If line <> "" Then
      linenr = linenr + 1
      Reg.SetStringValue(HKCUkey,"Line " & linenr,line)
   End If
   For I = linenr + 1 To 15
      Reg.SetStringValue(HKCUkey,"Line " & I,"")
   Next I
End Sub

Sub Timer1_tick
   Timer1.Enabled = False
   Hardware1.ShowTodayScreen
   Timer2.Enabled = True
   Timer2.Interval = 60000   ' 1 minuut
End Sub

Sub Timer2_tick
End Sub

Sub Word(strString,wordNr,separator,default)
   Dim Result, words(0) As string 
   If separator = "" Then separator = "|"
   Result = default 
   words() = StrSplit(strString & separator, separator)
   If wordNr <= ArrayLen(words())-1 Then Result = words(wordNr-1)
   Return Result
End Sub
 

mjcoon

Well-Known Member
Licensed User
I found out, somewhere in the forum, that I need a timer of sorts to keep the program running in the background.

Which forum item was that? Was the purpose to stop the device switching off rather than directly to do with your program?

Mike.
 

RobVanBrunschot

Member
Licensed User
Longtime User
Which forum item was that? Was the purpose to stop the device switching off rather than directly to do with your program?

Mike.

The purpose is intercepting an SMS when it comes in and do something with it.
But when the program is waiting for a new sms in the background, via the timer, i am afraid it will consumes to much battery energy.

Regards, Rob
 

agraham

Expert
Licensed User
Longtime User
As long as your application is not actually running code but is waiting for a timer it should not be increasing the use of battery power more than is normally being used when the device is switched on but is idling. Have you tried it to see if battery life is affected?
 

RobVanBrunschot

Member
Licensed User
Longtime User
agraham,

Thanks for the response,

i will try to run the program and see if it has any affect.
I will get back about that.

regards, Rob
 

RobVanBrunschot

Member
Licensed User
Longtime User
agraham,

Thanks for the response,

i will try to run the program and see if it has any affect.
I will get back about that.

regards, Rob

agraham,

I have the program running now for some 24 hours and it does not consume battery, as you said. I will continue with the program.

regards, Rob
 
Top