#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: 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.
Public PhoneEvent1 As PhoneEvents
Public kvs As KeyValueStore
Public bInternet As Boolean
Public bWiFi As Boolean
Public strInternetState As String
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
Dim jo As JavaObject
kvs.Initialize(File.DirInternal, "DataStorage")
PhoneEvent1.Initialize("PhoneEvent")
jo.InitializeStatic("java.util.Locale").RunMethod("setDefault", Array(jo.GetField("UK")))
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
LogError(Error.Message, StackTrace)
Return True
End Sub
Sub LogError(strErrorMsg As String, strStackTrace As String)
Log("Starter, LogError, strErrorMsg: " & strErrorMsg)
Dim arrError(3) As Object
arrError(0) = DateTime.Now / 1000 'as we need seconds, not milli-seconds
arrError(1) = GetErrorSubFromStackTrace(strStackTrace)
arrError(2) = strErrorMsg
CallSub2(B4XPages.GetManager.MainPage, "LogAppError", arrError)
End Sub
Sub GetErrorSubFromStackTrace(strStackTrace As String) As String
Dim iPos1 As Int
Dim iPos2 As Int
'Enums.strPackageName: b4a.exampleljjll
iPos1 = strStackTrace.IndexOf(Enums.strPackageName) + Enums.strPackageName.Length + 1
iPos2 = strStackTrace.IndexOf2("(", iPos1)
Return strStackTrace.SubString2(iPos1, iPos2).Replace("_", "")
End Sub
Sub Service_Destroy
End Sub
Sub PhoneEvent_ConnectivityChanged(NetworkType As String, State As String, Intent As Intent)
'NetworkType - WIFI or MOBILE.
'State - One of the following values: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN.
If State = "CONNECTED" Then
bInternet = True
Else
strInternetState = State
bWiFi = (NetworkType = "WIFI")
End If
End Sub