The Service Module keep recreate and then Error

jeeradate

Member
Licensed User
Longtime User
My App let user set the duration in Mian.DulationV and pass to the Service Module via Global variable to keep running every interval by StartServiceAT.

But sometime the sub Service_Create had been called in between and Main.DulationV = 0 then program is error.

Would you please be so kind and advise?
:sign0104:
(Sorry for my poor English, I am Thai)

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Medi-Bell2
    #VersionCode: 1
    #VersionName: 
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    
#End Region

#Region Activity Attributes
    #FullScreen: True
    #IncludeTitle: True

#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim DulationV As Int
    Dim TextV As String

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.
    Dim btnStart As Button
    Dim btnStop As Button
    Dim Label1 As Label
    Dim SeekBar1 As SeekBar
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("Note10")
    If FirstTime Then
        TextV = "2 Minutes"
        DulationV = 120
    End If
End Sub

Sub Activity_Resume
    If IsPaused(MService)=True Then
        btnStart.Enabled = True
        btnStop.Enabled = False        
    Else
        btnStart.Enabled = False
        btnStop.Enabled = True
    End If 
    SeekBar1.Value = DulationV/10
    Label1.Text = TextV
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
End Sub

Sub btnStop_Click
    CancelScheduledService(MService)
    StopService(MService)
    btnStart.Enabled = True
    btnStop.Enabled = False
    
End Sub
Sub btnStart_Click
    StartService(MService)
    Activity.Finish


End Sub

Sub SeekBar1_ValueChanged (Value As Int, UserChanged As Boolean)
    Dim MinuteV As Int
    Dim SecondV As Int
    If Value < 2 Then
        Value = 2
        SeekBar1.Value = 2
    End If
    
    SecondV = Value*10 Mod 60
    MinuteV = (Value*10 - SecondV)/60
    TextV = ""
    If MinuteV > 0 Then
        TextV = MinuteV & " Minutes "
    End If
    If SecondV >0 Then
        TextV = TextV & SecondV & " Second"
    End If 
    Label1.Text = TextV
    DulationV = Value*10
End Sub
B4X:
#Region Module Attributes
    #StartAtBoot: False
#End Region

'Service module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim MP As MediaPlayer
    Dim DT As Int
    Dim N As Notification
End Sub
Sub Service_Create
    MP.Initialize()
    MP.Load(File.DirAssets,"bell10s.ogg")
    DT = Main.DulationV
    If DT = 0 Then
        DT = 120
        Log( "Main.DulationV = 0 again")
    End If
    n.Initialize
    n.Icon = "icon"
    n.Sound = False
    n.Vibrate= False
    n.Light=False
    n.OnGoingEvent=True    
    n.SetInfo("Mindfulness Bell", "Interval= " & Main.TextV &" First=" & DateTime.Time(DateTime.now)   , Main)
    Log("First Start = " & DateTime.Time(DateTime.now))
    n.Notify(1)    
End Sub

Sub Service_Start (StartingIntent As Intent)
    MP.Play
    n.SetInfo("Mindfulness Bell", "Interval= " & Main.TextV &" Last=" & DateTime.Time(DateTime.now)   , Main)
    Log(DateTime.Time(DateTime.now))
    n.Notify(1)    
    Log("DT = "& DT)
    Log("DulationV = "& Main.DulationV)
    If DT > 110 Then 
        StartServiceAt("",DateTime.Now+DT*DateTime.TicksPerSecond,True)
    Else
        Log(" DT error  = "&DT)
        Service_Destroy
    End If
End Sub

Sub Service_Destroy
    CancelScheduledService("")
    StopService("")
    n.Cancel(1)    
End Sub
The log as following

B4X:
LogCat connected to: B4A-Bridge: samsung GT-N8000-351896050942487
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Installing file.
PackageAdded: package:noklek.com.medibell2
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (mservice) Create **
First Start = 10:17:05
** Service (mservice) Start **
10:17:05
DT = 120
DulationV = 120
** Activity (main) Pause, UserClosed = true **
** Service (mservice) Create **
Main.DulationV = 0 again
First Start = 10:19:05
** Service (mservice) Start **
10:19:05
DT = 120
DulationV = 0
** Service (mservice) Start **
10:21:05
DT = 120
DulationV = 0
** Service (mservice) Start **
10:23:05
DT = 120
DulationV = 0
 

Attachments

  • medibell.zip
    91.2 KB · Views: 177
  • medibell.apk
    184.9 KB · Views: 167

Erel

B4X founder
Staff member
Licensed User
Longtime User
Eventually the process will get killed. Now when the service is restarted (because of the StartServiceAt call) DurationV will be 0. The solution is to save the value to a file and then read the value from a file. StateManager can be used to save such settings (though a simple File.WriteString / ReadString is also fine).
 
Upvote 0

jeeradate

Member
Licensed User
Longtime User
Eventually the process will get killed. Now when the service is restarted (because of the StartServiceAt call) DurationV will be 0. The solution is to save the value to a file and then read the value from a file. StateManager can be used to save such settings (though a simple File.WriteString / ReadString is also fine).

Thank you very much for your kind support.
I will use File.WriteString / ReadString.

:sign0142:

I just wonder why OS kills the service module as it still active with StartServiceAt and I have 2 GB RAM in SSG Note with only few app opened. And what is the use for Sub Process_Globals as the Active App will be killed.
 
Last edited:
Upvote 0
Top