Need help with service.

VITMan

Member
Licensed User
Longtime User
I have a service that uses SMSIntercept to catch incoming text messages and to trigger a notification.

I want the main activity to open up and display the text in a big label or edittext when the user presses on the notification.

I'm having trouble with the activity opening and displaying the message.

Here is the Activity Module

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.
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 lblMessage As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layoutMain")
   PopUp_TextMsg
End Sub

Sub Activity_Resume
   SMS_Start
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SMS_Start
   If SMSTest.servicerunning = False Then
       StartService(SMSTest)
       SMSTest.servicerunning = True
       ToastMessageShow("VIT-SMS is running in background", True)
   End If
End Sub

Sub PopUp_TextMsg()
   Activity.BringToFront
   lblMessage.Text = SMSTest.mFrom & CRLF & CRLF & SMSTest.mText
   Msgbox(SMSTest.mText, SMSTest.mFrom)
End Sub

And here is the Service Module

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 servicerunning As Boolean
   Dim notification1 As Notification
   Dim smsInt As SmsInterceptor
   
   Dim mFrom As String
   Dim mText As String
End Sub

Sub Service_Create
   smsInt.Initialize2("smsInt", 999)
    notification1.Initialize
    notification1.Icon = "icon"
    notification1.Vibrate = True
   notification1.Sound = True
    Log("Service initialized")
   
End Sub

Sub Service_Start (StartingIntent As Intent)
   StartServiceAt("", DateTime.Now + 10 * DateTime.TicksPerSecond, True)
    Log("Service Started")   
End Sub

Sub Service_Destroy
   smsInt.StopListening()
End Sub

Sub smsInt_MessageReceived (From As String, Body As String) As Boolean
    Log("Message Received From: " & From)
   mFrom = From
   mText = Body
   notification1.SetInfo("From: " & mFrom, mText, Main)
   
   Return False
End Sub

I haven't found any example that seem to fit.

Any help would be most appreciated.

Thanks.
 

sanjeeve

Member
Licensed User
Longtime User
Activate Main activity

I think you need to activate the main activity.

B4X:
Sub smsInt_MessageReceived (From As String, Body As String) As Boolean
    Log("Message Received From: " & From)
    mFrom = From
    mText = Body
    notification1.SetInfo("From: " & mFrom, mText, Main)
     StartActivity("Main") 

'this might help too..

If IsPaused(Main) Then
 StartActivity(Main)
Else
 CallSub(Main, "PopUp_TextMsg")
End If
    Return False
End Sub
 
Upvote 0

VITMan

Member
Licensed User
Longtime User
Need help with service - Activate Main activity

Thanks for the reply, sanjeeve !

I believe I read that you shouldn't call an Activity from a service... ?

I will try what you've posted when I get home and let you know.

Again, thanks for the guidance.

VITMan
 
Upvote 0

VITMan

Member
Licensed User
Longtime User
calling activity in service

I did not see that post when I was searching for an answer.

Thanks again !

VITMan
 
Upvote 0

VITMan

Member
Licensed User
Longtime User
Doesn't work as expected

The new code doesn't work as expected.

I don't want the main activity to come up until the user slides down the notifications and then presses the SMS notifcation generated by the notification1.SetInfo line...


B4X:
Sub smsInt_MessageReceived (From As String, Body As String) As Boolean
    Log("Message Received From: " & From)
    mFrom = From
    mText = Body
    notification1.SetInfo("From: " & mFrom, mText, Main)
End Sub


Am looking further into the notifications in the doc/forums but any help would be greatly appreciated.

Thanks.

VITMan
 
Upvote 0

VITMan

Member
Licensed User
Longtime User
Doesn't work as expected

Hi Erel,

Thanks ! Every tip or explanation really helps.

I will try this and see how it works.

Have been busy at work and haven't had time to go through the documentation and examples more.

Hopefully I'll have more time over the weekend.

VITMan
 
Upvote 0
Top