I'm stuck with service module

susu

Well-Known Member
Licensed User
Longtime User
I tried to write a little alarm app by using service module.

Main activity code:
B4X:
Sub SetSchedule_ItemClick (Position As Int, Value As Object)
   Dim start, now, atTime As Long
   DateTime.TimeFormat = "HH:mm"
   start = DateTime.TimeParse(Value)   ' parse time from xml, format like 14:32
   now = DateTime.Now
   
   If start > now Then
      atTime = start - now
      Msgbox("It's OK", "Set schedule")
      StartServiceAt(notischedule, atTime, False)
   Else
      Msgbox("Cannot set schedule in past time","Error")      
   End If
End Sub

Service module name "notischedule" code:
B4X:
Sub Process_Globals
   Dim Noti As Notification
End Sub

Sub Service_Create
   Noti.Initialize
   Noti.Icon = "icon"
   Noti.Light = True
   Noti.Sound = True
   Noti.Vibrate = True
   Noti.AutoCancel = True
End Sub

Sub Service_Start
   Noti.SetInfo("Hey, it's time now!", "Alarm", Main)
   Noti.Notify(1)
End Sub

Compile ok but it doesn't work and there's no notification icon. What did I miss? Please help.
 

susu

Well-Known Member
Licensed User
Longtime User
Thanks Agraham, I edited my code:

StartServiceAt(notischedule, DateTime.Now + atTime, False)

but it doesn't work.
Can anyone share an alarm example?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Hi Cor, the code is in first post because it's just a test app.
I don't know why there's no icon in notification bar.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
There's nothing in LogCat :(
Can you give me an alarm example?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Yes, I did read it and wrote the code as below but I can't make it work :(
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is a very simple example that starts the service in 10 seconds:
B4X:
Sub Process_Globals
End Sub
Sub Globals

End Sub
Sub Activity_Create(FirstTime As Boolean)
    StartServiceAt(s1, DateTime.Now + 10 * DateTime.TicksPerSecond, False)
End Sub
Service module named S1:
B4X:
Sub Process_Globals
    Dim Noti As Notification
End Sub

Sub Service_Create
    Noti.Initialize
    Noti.Icon = "icon"
    Noti.Light = True
    Noti.Sound = True
    Noti.Vibrate = True
    Noti.AutoCancel = True
End Sub

Sub Service_Start
    Noti.SetInfo("Hey, it's time now!", "Alarm", Main)
    Noti.Notify(1)
End Sub
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thanks Erel, it seems to work but I got error in LogCat:

Unable to start service Intent { flg=0x4 cmp=abc.xyz.test/.s1 (has extras) }: not found

My service module is named "s1"
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Sorry Cor, this is my next project to publish to Market so I can't post it here. I can mail you zip file if you PM me your email :D
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thanks Erel, now I know what's the problem.

When we use Service, AndroidManifest.xml will be modified and in my case it's read-only (because I used AdMob lib before). I think you should mention about AndroidManifiest.xml in Service tutorial.

More questions: How to set 3 alarms on 3 different times? How to list down all the alarms that set? How to remove 1 alarm? And why there's no document about Service (I only found your tutorials).
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
Thanks Erel.
I used so many alarm apps that I can set 3-4 or even 5 alarm schedule. How did they do that?
 
Upvote 0
Top