Service is closing Unexpectedly
Hello,
I am developing an app, where service module listens to various system events, but after first "smsReceived_MessageReceived" event, the service doesn't responds to another "smsReceived_MessageReceived" event. Here is a sample of my Service Module
And in the ReplyTo activity, on a button onClick event, i am using Activity.Finish to close the activity. But the Service should be running in the background right?
:sign0163: :sign0085: please
Hello,
I am developing an app, where service module listens to various system events, but after first "smsReceived_MessageReceived" event, the service doesn't responds to another "smsReceived_MessageReceived" event. Here is a sample of my Service Module
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim pEvents As PhoneEvents 'for screen on/off
Dim smsReceived As SmsInterceptor ' for receiving SMS
Dim cToast As CustomToast 'For displaying custom toast
Dim screenOn As Boolean
Dim fromMessage, fromNumber As String
Dim msgPending As Boolean
Dim pSensor As PhoneSensors
Dim ProxTimer As Timer
Dim pWakeState As PhoneWakeState
End Sub
Sub Service_Create
pEvents.Initialize("pEvents")
smsReceived.Initialize("smsReceived")
End Sub
Sub Service_Start (StartingIntent As Intent)
screenOn = True
msgPending = False
'maxProxValue = pSensor.MaxValue
ToastMessageShow("TextNotification Service Started.", True)
End Sub
Sub Service_Destroy
ToastMessageShow("TextNotification Service Stopped.", True)
pEvents.StopListening
End Sub
Sub pEvents_ScreenOn (Intent As Intent)
Log("Screen On")
screenOn = True
If msgPending = True Then
Log("message is pending, so displaying a toast now.")
dispToast(fromNumber & ": " & fromMessage)
msgPending = False
ScreenAwoke
End If
End Sub
Sub pEvents_ScreenOff (Intent As Intent)
Log("Screen Off")
screenOn = False
End Sub
Sub ScreenAwoke
' Screen On, therefore, turning off the Prox Sensor and Phone Wake State
pSensor.StopListening
pWakeState.ReleaseKeepAlive
End Sub
Sub smsReceived_MessageReceived (from As String, Body As String) As Boolean
Log("From: " & from & ". Body: " & Body)
fromMessage = Body
fromNumber = from
If screenOn = True Then
dispToast(fromNumber & ": " & fromMessage)
Else
' screen is off
msgPending = True
pSensor.Initialize(pSensor.TYPE_PROXIMITY)
ProxTimer.Initialize("ProxTimer", 5000)
ProxTimer.Enabled = True
pSensor.StartListening("pSensor")
End If
End Sub
Sub pSensor_SensorChanged (Values() As Float)
Dim SensorValue As Int
SensorValue = Values(0)
Log("SensorValue: " & SensorValue)
' value = 0 then object in proximity
' value = maxValue then object not in proximity
' turning the screen on.
'If SensorValue = 0 Then
If SensorValue < pSensor.MaxValue Then
pWakeState.KeepAlive(True)
StartActivity(ReplyTo) ' Starting the Reply To Activity
End If
End Sub
Sub ProxTimer_Tick
ProxTimer.Enabled = False
Log("Maximum Time Reached. Closing the proximity sensor and releasing the KeepAlive.")
' turning the Proximity off
pSensor.StopListening
' realeasing the screen alive
pWakeState.ReleaseKeepAlive
End Sub
Sub dispToast (message As String)
cToast.Initialize
'cToast.Show(message, 10000, Gravity.BOTTOM, 0, 60)
cToast.Show(message, 10000, Gravity.CENTER, 0, 0)
End Sub
And in the ReplyTo activity, on a button onClick event, i am using Activity.Finish to close the activity. But the Service should be running in the background right?
:sign0163: :sign0085: please
Last edited: