Android Question Problems with variables initialized in Starter service

FrankBerra

Active Member
Licensed User
Longtime User
Hi all

I am using Starter service for initializing variables/Databases, etc...
For example i am initializing database in this way:
B4X:
Database.Initialize(File.DirDefaultExternal, "settings.db", True)

and everything seems to work properly almost always, but sometimes i am facing to strange problems.


After some suspects i put some checks in other services than Sarter Service like this:
B4X:
If (Starter.Database.IsInitialized == True) Then
    
    .....<My codes here>.....

Else
     logger.Initialize(File.OpenOutput(File.DirDefaultExternal, "Logger.txt", True))
     logger.WriteLine("Database not initialized?!?!?!")
     logger.Close
End If

and sometimes in file Logger.txt i find that the database is not initilized.

My question is: why the database seems not initialized even if it is initialized in starter service?
Could be there something wrong that de-initilizes the database?
 

FrankBerra

Active Member
Licensed User
Longtime User
Yes, all variables are initialized in service_create (while the subs Service_Start and Service_Destroy are empty)
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
The app can be started by clicking it's icon but most of the time the app starts at boot with a sticky service.

I can post the logs but they are "home-made" so they don't give you any useful information, for example:
B4X:
.....
.....
Routine Restart started
UsersA Found: 0
Average Users: 0
UsersB found: 36
Distance: 7.4305901527404785
Restart in 4 minutes
Database not initialized?!?!?!
16:56:33
status: 1
.....
.....


I dont know how to close a database, so i think i am not closing it intentionally.

I have to say that the code i posted in the first post is just part of an another If-THEN:

B4X:
If StartingIntent.IsInitialized And StartingIntent.Action = "android.net.conn.CONNECTIVITY_CHANGE" Then
   If (Starter.Database.IsInitialized == True) Then

       .....<My codes here>.....
   Else
       logger.Initialize(File.OpenOutput(File.DirDefaultExternal, "Logger.txt", True))
       logger.WriteLine("Database not initialized?!?!?!")
       logger.Close
   End If
Else
....
End If
So, is it possible that the broadcast receiver works even if the starter service not completed the initialization?
Anyway the problems doesn't occour so often but it happens.

Sometimes when i use codes like the following for getting username and password from database and i post it to server, the server says me that i sent empty/null values even if username and password are there stored in database:

B4X:
Riautenticazione.PostString(Starter.IndirizzoServer & Starter.VersioneApp & "/Riauth.php", "user=" & Funzioni.ImpostazioniGET("user") & "&password=" & Funzioni.ImpostazioniGET("pass"))

where ImpostazioniGET is:
B4X:
Sub ImpostazioniGET(NomeImpostazione As String) As String
  
    Dim ValoreDaRestituire As String
 
    Dim CursoreImpostazioni As Cursor
 
    Try
        CursoreImpostazioni = Starter.DatabaseImpostazioni.ExecQuery2("SELECT [" & NomeImpostazione & "] FROM impostazioni WHERE user = ?",  Array As String(Starter.Username))
    Catch
        Starter.DatabaseImpostazioni.ExecNonQuery("ALTER TABLE impostazioni ADD COLUMN [" & NomeImpostazione & "] TEXT")
        Starter.DatabaseImpostazioni.Initialize(File.DirDefaultExternal, "settings.db", True)
        ValoreDaRestituire = Null
      
        Return ValoreDaRestituire
    End Try
 
 
    If CursoreImpostazioni.RowCount > 0 Then
        CursoreImpostazioni.Position = 0
        ValoreDaRestituire = CursoreImpostazioni.GetString(NomeImpostazione)
    Else
        ValoreDaRestituire = Null
    End If
  
    CursoreImpostazioni.Close
    Return ValoreDaRestituire
 
End Sub
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Sorry but it is a big project with more 20000 lines of code and i don't feel confortable to share it.

I think that these problems are related to some crashes that i can't log. Maybe when the app/service crashes the starter service reinitializes everything and in meanwhile the database appears not initialized

I use this for catching errors but the file CrashReport.txt contain just the string "============":
B4X:
Sub Application_Error (Error AsException, StackTrace AsString) As Boolean
  Dim jo AsJavaObjectDim l As Long = 500
  jo.InitializeStatic("java.lang.Thread").RunMethod("sleep", Array(l))
  logcat.LogCatStop
  logs.Append(StackTrace)
  File.WriteString(File.DirDefaultExternal, "CrashReport.txt", logs)
  File.WriteString(File.DirDefaultExternal, "CrashReport.txt", Chr(10) & Chr(10) & "============================" & Chr(10) & Chr(10))
  ReturnTrue
End Sub

And strangely i can't spot anything in logcat (sometimes because i check logs too late o because logcat started to log only after the problems happened like it didn't log anything for hours...)
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
How do you know the OS is not stopping the service?

B4X:
if ispaused(ServiceName) then
  stopservice(ServiceName)
  startservice(ServiceName)
end if
 
Upvote 0
Top