Hello, I need a help from the community experts.
I'm trying to make an app that starts a service to show a notification in a predefined time, some hours later.
I tried to make a test program using what I found is the logical way. But I know that I'm doing something wrong. Nothing works as I want.
This example program was supposed to start a service when the button1 is pressed, and shows a message in the log some seconds later, then stop the service.
Button2 was supposed to stop the service until button1 is pressed and starts again at a new time.
The service was meant to be destroyed when the application is closed, running only when the activity is active.
Running this test program gives strange results: the service starts some seconds after the application starts, without pressing any button, Then, it keeps starting again and again.
I looked at the forum but could not find an very simple example or tutorial to understand the way this works, only more complex examples (for me, at least), like the background download and twitter.
If someone can point in my code what I'm doing wrong I'll be very glad. Thanks for any help.
I know that using a timer is an easy way to do this, but I can't use a timer, I need a service, I really need to understand how services work, so this program is only an simple example to demonstrate and test.
My code is attached as project.zip.
Main Activity code:
Test Service code
I'm trying to make an app that starts a service to show a notification in a predefined time, some hours later.
I tried to make a test program using what I found is the logical way. But I know that I'm doing something wrong. Nothing works as I want.
This example program was supposed to start a service when the button1 is pressed, and shows a message in the log some seconds later, then stop the service.
Button2 was supposed to stop the service until button1 is pressed and starts again at a new time.
The service was meant to be destroyed when the application is closed, running only when the activity is active.
Running this test program gives strange results: the service starts some seconds after the application starts, without pressing any button, Then, it keeps starting again and again.
I looked at the forum but could not find an very simple example or tutorial to understand the way this works, only more complex examples (for me, at least), like the background download and twitter.
If someone can point in my code what I'm doing wrong I'll be very glad. Thanks for any help.
I know that using a timer is an easy way to do this, but I can't use a timer, I need a service, I really need to understand how services work, so this program is only an simple example to demonstrate and test.
My code is attached as project.zip.
Main Activity code:
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim wtime As Long
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.
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("Layout1")
If FirstTime Then
StopService("Test")
Log("Stop")
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
'StopService("Test")
End Sub
Sub Button1_Click
wtime = DateTime.Now+5000
StartService("Test")
Log("BTStart " & DateTime.time(wtime))
End Sub
Sub Button2_Click
StopService("Test")
Log("BTStop")
End Sub
Test Service code
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim n As Int
End Sub
Sub Service_Create
Log("Create")
n=0
Log(n)
End Sub
Sub Service_Start (StartingIntent As Intent)
Log("Start")
n=n+1
Log(n)
StartServiceAt("",Main.wtime,False)
End Sub
Sub Service_Destroy
n=n+1
Log(n)
Log("Destroy")
End Sub
Attachments
Last edited: