Android Question Help with starting service with startserviceat

demasi

Active Member
Licensed User
Longtime User
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:
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

  • project.zip
    7.5 KB · Views: 223
Last edited:

LucaMs

Expert
Licensed User
Longtime User
the service starts some seconds after the application starts, without pressing any button,
Not in the project you attached.


it keeps starting again and again.
Because the scheduled time you set with StartServiceAt is a fixed time and it will always be a time expired (except the first time, when you click on button1).

B4X:
Sub Service_Start (StartingIntent As Intent)
    Log("Start")
    n=n+1
    Log(n)
    StartServiceAt("", DateTime.Now + 5000, False)
End Sub

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        LogColor("Stopping service", Colors.Red)
        StopService("Test")
        CancelScheduledService("Test")
    End If
End Sub

Sub Button2_Click
   StopService("Test")
   CancelScheduledService("Test")
   Log("BTStop")
End Sub
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Hello, LucaMs.
Finally I learned how to do what I need.
It´s very simple.
I used "StartAt" in the start button, with the time I need to start the service.
Then, in the service code, i just put the code I need.
Worked like a charm.
I was complicating a simple task.
Thanks for your help.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…