Android Question Question about background process

kohle

Active Member
Licensed User
Longtime User
I have declared in the main activity at process globals
a list : Dim tempSensors As List
Init. is in activity_create.

I have a Service : BluetoothReceiver where I receive data.
Here I use a Function from another class called CL_CENTRAL
The function I call : translate_code from CL_CENTRAL

Here I access the List "tempSensors" from the main activity process globals.
If Main.tempSensors.IndexOf(strCode.SubString2(0,4) )> -1 Then ....


The app is running for a while without problems, specially in debug mode. In release mode
from time to time, after several hours, I get this error code.

It looks like that the main activity is not initialized anymore and the service still running
I thought process globals are not killed.

How I can solve the problem.


B4X:
sending message to waiting queue of uninitialized activity (setstate)
cl_central_translate_code (java line: 813)
java.lang.RuntimeException: Object should first be initialized (List).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.objects.collections.List.IndexOf(List.java:135)
    at jk.central433.base.cl_central._translate_code(cl_central.java:813)
    at jk.central433.base.bluetoothreceiver$ResumableSub_ast_NewText.resume(bluetoothreceiver.java:293)
    at jk.central433.base.bluetoothreceiver._ast_newtext(bluetoothreceiver.java:183)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1164)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
** Receiver (bluet

B4X:
'class: cl_central

public Sub translate_code(strCode As String) As String
    
    
    Dim ret As String
    Dim po As Int
    Dim ls_Sql As String
    Dim prfx As String
    
    Dim cu As Cursor

    Dim flgTemp As Int
    Dim TempCelsius As String

    ret = strCode
    
    
    strCode=getOnlyCode(strCode)
    
    
    If strCode.Length=6 Then
        If  Main.tempSensors.IndexOf(strCode.SubString2(0,4) )> -1  Then
            flgTemp=1
            TempCelsius= strCode.SubString(strCode.Length-2)
            strCode= strCode.SubString2(0,4)
        End If
    End If
    
    
    ls_Sql="select code,description,id_action  from t_code  where code='" & strCode & "'"
    cu=Main.jk.SQL.ExecQuery(ls_Sql)
    
    If cu.RowCount=0 Then
        
        ls_Sql="select description  from t_remote  where remote_codes ='" & strCode & "'"
        cu=Main.jk.SQL.ExecQuery(ls_Sql)
    
        If cu.RowCount=0 Then
           Return ret
        End If
        
        prfx = "RM "
        
    End If   
    
    
    cu.Position=0
    
    If flgTemp=1 Then
        prfx="TMP "
        ret = prfx & cu.GetString("description") & " " & TempCelsius
    Else
        ret = prfx & cu.GetString("description")
    End If
    
    
    cu.Close
    
    Return ret
    
    
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Important example: Background location tracking

You should have a foreground service.

I guess that in your app the process is killed and then recreated. Activity_Create will not run when that happens so you will get this error.

As a general rule: the only place where you should have public process global variables is in the starter service, and the variables should be initialized in Service_Create.
 
Upvote 0

kohle

Active Member
Licensed User
Longtime User
Ok, I understand. I have one more question. In the main activity I have a timer running every 30min.
I thought it should prevent the main activity to be killed.
But I will try with a sheldued service and move the globals to the starter service.
 
Upvote 0
Top