Android Question Use service to restart crashed app

Rick Harris

Well-Known Member
Licensed User
Longtime User
I am trying to create a watchdog that monitors my app and restarts my app if it crashes.
My app writes the datatime value every 10 seconds into a file in dirRootexternal. A service reads this file every X seconds to check whether my app is still saving datetime. If not, my app has probably crashed.
So far so good, however the service stops working while my app shows an error message on the screen.
Is there a way to solve this, i.e. to keep the service running at all times?
 

Rick Harris

Well-Known Member
Licensed User
Longtime User
@JakeBullet70 is correct. The whole process is killed when the app crashes. You can try to create a sticky service. Android will then recreate the service if it crashes (though it may stop if it crashes many times).
Thank you both. Indeed I have resolved it and now have a good watchdog service running which restarts my app in the rare cases that it crashes. If anyone is interested, you can ask me for the code.
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Hey Rick.

I would love a copy of the code.
My app too runs 24/7 and does once in a while crash, not too often... :)
I was thinking that soon I too would have to right something like this. :)

--Jake
 
Upvote 0

Rick Harris

Well-Known Member
Licensed User
Longtime User
Hey Rick.

I would love a copy of the code.
My app too runs 24/7 and does once in a while crash, not too often... :)
I was thinking that soon I too would have to right something like this. :)

--Jake

Here is the sourcecode for the watchdog app:
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Watchdog
    #VersionCode: 1
    #VersionName: 1.01
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region
 
Sub Process_Globals
End Sub
 
Sub Globals
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
    StartService(RunService1)
    Activity.Finish
End Sub
 
Sub Activity_Resume
End Sub
 
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Here is the sourcecode for the service (called "RunService1"):
B4X:
#Region Module Attributes
    #StartAtBoot: True
#End Region
 
Sub Process_Globals
    Dim Timer1 As Timer
End Sub
 
Sub Service_Create
    Timer1.Initialize("Timer1",10000)  '10 sec. watchdog
    Timer1.Enabled = True
End Sub
 
Sub Service_Start (StartingIntent As Intent)
    StartServiceAt("", DateTime.Now + 120 * DateTime.TicksPerSecond, True)    'Restart after 2 minutes. This avoids that the Android O.S. kills the service after a while!
End Sub
 
Sub Service_Destroy
    Service.StopForeground(1)
    Timer1.Enabled = False
End Sub
 
Sub Timer1_tick
    If File.Exists(File.DirRootExternal, "ArtiRun1.dsh") Then
      Dim Dhulp1 As String
      Dim Ticks1 As Long
      Dim Ticks2 As Long
 
      Dhulp1 = File.ReadString(File.DirRootExternal, "ArtiRun1.dsh")
      If Dhulp1 = "0" Then
        'StopService(Me)        'Don't do this because watchdog service is always running(started at Boot)
        Return
      End If
 
      Ticks1 = Dhulp1
      Ticks2 = DateTime.Now
      ' ToastMessageShow(((Ticks2-Ticks1)/1000) & " sec.",True)
 
      If Ticks2-Ticks1>30000 Then    '30 sec no activity by main App, so it probably has crashed. The main app stores datetime.now tick every 10 seconds in ArtiRun1.dsh file
        ToastMessageShow("RESTARTING APP",True)
 
        Dim Intent1 As Intent
        Dim pm As PackageManager
        Intent1 = pm.GetApplicationIntent("[app name of your application]")  'Restart
        StartActivity(Intent1)
 
        Ticks1 = DateTime.Now
        Dhulp1 = Ticks1
        Dim TW As TextWriter
          TW.Initialize(File.OpenOutput(File.DirRootExternal, "ArtiRun1.dsh", False))
          TW.Write(Dhulp1)
        TW.Close
      End If
    End If
End Sub

In the main App that needs to be watched: add a timer that fires approximately every 10 seconds:

B4X:
Sub Timer1_tick
  SAVE_RUNNING1(DateTime.Now)
end sub
 
Sub SAVE_RUNNING1(inData As String)
    Dim TW As TextWriter
      TW.Initialize(File.OpenOutput(File.DirRootExternal, "ArtiRun1.dsh", False))
      TW.Write(inData)
    TW.Close
End Sub

When your app closes make sure to do the following:
B4X:
      Timer1.Enabled = False
SAVE_RUNNING1("0")        'This makes the watchdog service stop as well, provided you include the StopService(Me) code line mentioned above.
I hope I have not forgotten anything because I had to strip this code from my own (much more elaborate) app.
 
Last edited:
Upvote 0

Phayao

Active Member
Licensed User
Longtime User
Hi, i think it is the package name - something like "com.android.program" (use " around it)
Good Luck, i'll try this concept as well.
 
Upvote 0

Rick Harris

Well-Known Member
Licensed User
Longtime User
I am now trying to integrate my Watchdog into a service that is started from within the app itself (in stead of using a separate Watchdog app as described above). However, such a service seems to freeze if the main activity crashes. Am I mistaken or am I doing something wrong?
 
Upvote 0
Top