Android Question Service to background

AHilberink

Active Member
Licensed User
Longtime User
Hi,

Can I run a service completly in the background?

This is my Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        StartService(Adresboek)
    End If

This is my 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 cu As ContactsUtils
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Vullen_Adresboek
End Sub

Sub Service_Destroy

End Sub

Sub Vullen_Adresboek
    ........
End Sub

On opening the FirstTime it seems to wait till the service is done or is this incorrect? Can it start the service without waiting to complete?

BR, André
 

AHilberink

Active Member
Licensed User
Longtime User
It doesn't wait for the service to be created. In fact, the service will only be created after Activity_Create completes.

Thanks for your reply.

I don't know if I understand it correctly, because I experience a delay caused by the "Vullen_Adresboek" in the Service_Start. My application opens with a blanc screen, runs the Service_Start and after this my application layout is displayed.

Is it possible to run the Service_Start in the background, while the main is continuïng and shows the layout directly?

BR, André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Hi Erel.

Of course.

This is it:
B4X:
*** Service (starter) Create ***
CreateTable: CREATE Table IF NOT EXISTS [Settings] ([ROWID] INTEGER DEFAULT 0 PRIMARY KEY, [CompanyID] TEXT DEFAULT '', [Gebruiker] TEXT DEFAULT '', [Wachtwoord] TEXT DEFAULT '', [Sleutel] TEXT DEFAULT '', [URL] TEXT DEFAULT '', [Versie] TEXT DEFAULT '')
CreateTable: CREATE Table IF NOT EXISTS [Relaties] ([ROWID] INTEGER DEFAULT 0 PRIMARY KEY, [vlag] TEXT DEFAULT '', [zoekterm] TEXT DEFAULT '', [nummer] INTEGER DEFAULT 0, [volgnummer] INTEGER DEFAULT 0, [naam] TEXT DEFAULT '', [contactpersoon] TEXT DEFAULT '', [bezoekadres] TEXT DEFAULT '', [postcode] TEXT DEFAULT '', [plaats] TEXT DEFAULT '', [telefoon] TEXT DEFAULT '', [mobiel] TEXT DEFAULT '', [email] TEXT DEFAULT '', [Foto] BLOB DEFAULT '')
ExecuteMap: SELECT * FROM Settings WHERE ROWID=1
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (adresboek) Create ***
** Service (adresboek) Start **
StartVullen - 12:16:11
DeleteRecord: DELETE FROM [Relaties] WHERE [vlag] = ?
InsertMaps (first query out of 254): INSERT INTO [Relaties] ([vlag], [zoekterm], [naam], [foto], [mobiel], [email], [telefoon]) VALUES (?, ?, ?, ?, ?, ?, ?)
EndVullen - 12:16:15

During StartVullen and EndVullen loading the layout is freezed. When I disable Starting the service there is no delay/freeze.

BR, André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Better to start this service from the starter service.

B4X:
Sub Service_Create
Sleep(3000)
StartService(Adresboek)
End Sub

Hi Erel,

This works for direct loading the layout, but during the run of the service the app is still freezing: I cannot use the App.

But it is a lot better. Thanks.

BR, André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Tell us what happens in Vullen_Adresboek. Maybe it can be optimized.

No problem:
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 cu As ContactsUtils
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    Vullen_Adresboek
End Sub

Sub Service_Destroy

End Sub

Sub Vullen_Adresboek
    Dim ListOfMaps As List
    Dim allContacts As List
    Dim Where As Map

    Log("StartVullen - "&DateTime.Time(DateTime.Now))

    Where.Initialize
    Where.Put("vlag","A")
    DBUtils.DeleteRecord(Main.SQL1,Main.DBRelaties,Where)
    
    ListOfMaps.Initialize
    cu.Initialize
    allContacts = cu.FindAllContacts(True)
    allContacts.SortType("DisplayName", True)

    For Each c As cuContact In allContacts
        Dim RecordMap As Map
        RecordMap.Initialize

        RecordMap.Put("vlag","A")
        RecordMap.Put("zoekterm",c.DisplayName.ToUpperCase)
        RecordMap.Put("naam",c.DisplayName)
        Dim ContactFoto As Bitmap
        ContactFoto=cu.GetPhoto(c.Id)
        If(ContactFoto.IsInitialized) Then
            RecordMap.Put("foto",EigenFuncties.ImageToBytes(ContactFoto))
        Else
            RecordMap.Put("foto","")
        End If
        Dim PhoneList As List
        PhoneList=cu.GetPhones(c.Id)
        If(PhoneList.Size>0) Then
            Dim Phonenr As cuPhone
            Phonenr = PhoneList.Get(0)
            RecordMap.Put("mobiel",Phonenr.Number)
        Else
            RecordMap.Put("mobiel","")
        End If
        Dim eMailList As List
        eMailList=cu.GetEmails(c.Id)
        If(eMailList.Size>0) Then
            Dim eMail As cuEmail
            eMail = eMailList.Get(0)
            RecordMap.Put("email",eMail.Email)
        Else
            RecordMap.Put("email","")
        End If
        RecordMap.Put("telefoon","")
        ListOfMaps.Add(RecordMap)
    Next
    DBUtils.InsertMaps(Main.SQL1,Main.DBRelaties,ListOfMaps)
    Log("EndVullen - "&DateTime.Time(DateTime.Now))
End Sub

Thanks,
André
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
A simple solution is to add Sleep(0) somewhere in the loop.

Hi Erel,

Thanks. This works.

For my information/knowledge:
Sleep(0) gives 0 millisecond pause, just for other processes to continue?
A really background proces is not possible in Android?

Anyway, thanks a lot for helping me out.

BR, André
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Sleep(0) gives 0 millisecond pause, just for other processes to continue?
Search for resumable subs as it is a bit more complicated.

A really background proces is not possible in Android?
Of course it is possible. You can modify ContactUtils to use ContentResolver.QueryAsync. It will require some work.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…