Android Question Check if service is started

imgsimonebiliato

Well-Known Member
Licensed User
Hello,
i use this code to start a service

Activity Main
B4X:
If FirstTime Then
    StartService("Svc")
End If
Service Module "Svc"
B4X:
Sub Service_Create
   Log("Service Started")
End Sub

Sub Service_Start (StartingIntent As Intent)
   StartServiceAt("", TomorrowAt10, False)
   BuildNotification
So the service start and I build a notification at TomorrowAt10.

But, if I close the app and re-open it, I have a new notification all times.
How can I prevent it?

I read with "IsPaused" and I tried to use it, but I can't find a valid solution.
Please Help :(:(:(:(:(
 

Straker

Active Member
Licensed User
You have a notification every time the app starts because when the code starts the service, the service is first scheduled at tomorroeAt10 and then you call build notification.
 

imgsimonebiliato

Well-Known Member
Licensed User
If I read exactly,
i must put
B4X:
StartServiceAt(Me, TomorrowAt10, False)
StartingIntent.Action will be "android.intent.action.BOOT_COMPLETED" 
[CODE]
at the beginning of the service in a try?
 

Straker

Active Member
Licensed User
See how your service starts:


B4X:
'Activity
If FirstTime Then          'whn your activity starts, you call the service
    StartService("Svc")
End If

Sub Service_Start (StartingIntent As Intent)  'Here the service starts
   StartServiceAt("", TomorrowAt10, False)    'The next re-start is schedulet for TomorrowAt10
   BuildNotification      'The notification pops up
As you can see, when you activity is created, you start the service- The service schedules itself for TomorrowAt10 and THEN you buildNotification. The buildNotification is ALWAYS executed when your app starts.

You should use StartServiceAt in your Activity_create.
 

imgsimonebiliato

Well-Known Member
Licensed User
It is a cycle.. The service must be always active.
And even If I set the StartServiceAt in the FirstTime, it is execute always when I re-start the app
 
Last edited:

Straker

Active Member
Licensed User
It is a cycle.. The service must be always active.
And even If I set the StartServiceAt in the FirstTime, it is execute always when I re-start the app
If you use StartServiceAt in the firsttime AND the service starts immediately (you get the notification) it simply means that the tomorrowAt10 value is somewhere in the past (not tomorrow) and for this reason the service starts immediately instead of wait for TomorrowAt10.

Where do you calculate TomorrowAt10?
I mean, are you sure the value is always what you're expecting? Try and log that value every time you use it.
 

imgsimonebiliato

Well-Known Member
Licensed User
I calculate TomorrowAt10 in the service module and I use the same value on the "FirstTime"
 

imgsimonebiliato

Well-Known Member
Licensed User
This line tells you whether the service was started because of the scheduled task:
B4X:
If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then
So where I put StartServiceAt?
 

imgsimonebiliato

Well-Known Member
Licensed User
And in the Main Activity? How can I call the service?
 
Top