Android Question (SOLVED) Service stop running after some time and then is back running

Xandoca

Active Member
Licensed User
Longtime User
Hi,

I've made a very simple app. User set two edittext (Running time and walk time) and then press start.
Start initialize a service that notify user after running time and then after walk time and then after running time and so on.
It works for some time and then stop running. after a long time the service back running again.

Any thoughts?

B4X:
'Main
Sub btnIniciar_Click
    If edtTempoCaminhada.Text <> "" AND edtTempoCorrida.Text <> "" Then
        tempoCaminhada = edtTempoCaminhada.Text
        tempoCorrida = edtTempoCorrida.Text
        StartService("Cronometro")
       
    Else
        Msgbox ("Entre com os tempos!","Erro")
    End If
End Sub


'Service
Sub Service_Create
   
  n.Initialize
  n.Icon = "icon"
  n.Vibrate = False
  n.OnGoingEvent = True
  n.SetInfo("Info", "Prevent from being killed", Main)
 
  Service.StartForeground(1,n)
  n2.Initialize
  n2.Icon = "icon"
  n2.Vibrate = False
  n2.AutoCancel = True
End Sub

Sub Service_Start (StartingIntent As Intent)
'    Dim volumeAtual As Int
    If Main.programaEmExecucao = "Sim" Then
        MediaPlayer1.Initialize( )
        Select Main.etapa       
            Case "Corrida"     
                '....

                n2.SetInfo("Info", "Correr", "")
                n2.Notify(1)
                MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "correr.mp3")
                MediaPlayer1.Play
                Main.etapa = "Caminhada"
                StartServiceAt("Cronometro",DateTime.Now+Main.tempoCorrida*1000,True)
                'StopService("Cronometro")

            Case "Caminhada"
                '....
                n2.SetInfo("Info", "Caminhar", "")
                n2.Notify(1)
                MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "caminhada.mp3")
                MediaPlayer1.Play
                Main.etapa = "Corrida"
                StartServiceAt("Cronometro",DateTime.Now+Main.tempoCaminhada*1000,True)
                'StopService("Cronometro")
        End Select
    Else
        Service.StopForeground(0)
        StopService("Cronometro")
    End If
End Sub

tks,
Riani
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
You should take a look at PhoneWakeState especially PartialLock to unable the CPU to go to sleep :)
 
Last edited:
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Hello,
You should take a look at PhoneWakeState especially PartialLock to unable the CPU to go to sleep :)
Thanks.

I've tried but I am making something wrong because it's not working yet.
I've included "Dim Phone As PhoneWakeState" in Main->Process_Globals and Phone.PartialLock at Activity_Create.
Should I include in service module instead of activity?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
You're welcome.
I usually put it inside the service which has to be kept alive but as you say sometime it won't work when the screen is locked so I add another service to be launched every ten minutes or so via StartServiceAt. Perhaps could you try
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Final code is:
Main
B4X:
#Region  Project Attributes
    #ApplicationLabel: AP-Apoio Caminhada Corrida
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#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 etapa As String  'pode ser Corrida ou Caminhada
    Dim tempoCaminhada As Int
    Dim tempoCorrida As Int
    Dim programaEmExecucao As String
    Dim Phone As PhoneWakeState
  
'    Dim MediaPlayer1 As MediaPlayer
  
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.

    Private btnIniciar As Button
    Private btnParar As Button
    Private edtTempoCaminhada As EditText
    Private edtTempoCorrida As EditText
  
  
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("Main")
    Phone.PartialLock
    etapa = "Caminhada"
    programaEmExecucao = "Sim"
'    MediaPlayer1.Initialize( )
'                MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "correr.mp3")
'            MediaPlayer1.Play
'            MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "caminhada.mp3")
'            MediaPlayer1.Play

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub btnParar_Click
    programaEmExecucao = "Não"
    Activity.Finish
End Sub

Sub btnIniciar_Click
    If edtTempoCaminhada.Text <> "" AND edtTempoCorrida.Text <> "" Then
        tempoCaminhada = edtTempoCaminhada.Text
        tempoCorrida = edtTempoCorrida.Text
        WriteTextWriter
        StartService("Cronometro")
        'CallSub2("Cronometro","MonitoraTempo",etapa)
      
    Else
        Msgbox ("Entre com os tempos!","Erro")
    End If
End Sub




Sub WriteTextWriter
    Dim TextWriter1 As TextWriter
    TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "ApoioCorridaCaminhada.txt", False))
    TextWriter1.WriteLine(programaEmExecucao)
    TextWriter1.WriteLine(tempoCaminhada)
    TextWriter1.WriteLine(tempoCorrida)
    TextWriter1.Close
End Sub


Service
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 Notification
   Dim n2 As Notification
   Dim etapa As String
   Dim tempoCaminhada As Int
   Dim tempoCorrida As Int
   Dim programaEmExecucao  As String
  
End Sub
Sub Service_Create
  
  
  n.Initialize
  n.Icon = "icon"
  n.Vibrate = False
  n.OnGoingEvent = True
  n.SetInfo("Info", "Prevent from being killed", Main)
  
  Service.StartForeground(1,n)
  n2.Initialize
  n2.Icon = "icon"
  n2.Vibrate = False
  n2.AutoCancel = True
     etapa = Main.etapa
  
End Sub

Sub Service_Start (StartingIntent As Intent)
  
   monitoratempo(etapa)

End Sub

Sub Service_Destroy
  
End Sub

Sub monitoratempo (etapatmp As String)
   Dim MediaPlayer1 As MediaPlayer
   etapa = etapatmp
   ReadTextReader
   If programaEmExecucao = "Sim" Then
     MediaPlayer1.Initialize( )
     Select etapa
       Case "Corrida"  
         '....

        n2.SetInfo("Info", "Correr", "")
        n2.Notify(1)
        MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "correr.mp3")
         MediaPlayer1.Play
         etapa = "Caminhada" 'Main.etapa = "Caminhada"
         n2.Cancel(1)
         'StartServiceAt("Cronometro",DateTime.Now+Main.tempoCorrida*1000,True)
         StartServiceAt("Cronometro",DateTime.Now+tempoCorrida*1000,True)
         'StopService("Cronometro")

       Case "Caminhada"
         '....
        n2.SetInfo("Info", "Caminhar", "")
        n2.Notify(1)
         MediaPlayer1.Load(File.DirRootExternal  & "/ApoioCaminhadaCorrida/", "caminhada.mp3")
         MediaPlayer1.Play
         etapa = "Corrida"'Main.etapa = "Corrida"
         n2.Cancel(1)
         StartServiceAt("Cronometro",DateTime.Now+tempoCaminhada*1000,True)
         'StopService("Cronometro")
     End Select
   Else
     Service.StopForeground(0)
     StopService("Cronometro")
   End If

End Sub




Sub ReadTextReader
  Dim TextReader1 As TextReader
  TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "ApoioCorridaCaminhada.txt"))
   programaEmExecucao = TextReader1.ReadLine
  tempoCaminhada = TextReader1.ReadLine
  tempoCorrida = TextReader1.ReadLine  
  TextReader1.Close
End Sub
 
Upvote 0
Top