Android Question StartService HttpUtils2Service when auto destroy it

netsistemas

Member
Licensed User
I need the service HttpUtils2Service in starter service, because use method JOBDONE(Job as HTTPJO)

All service are destroyed in ANDROID8+ (ok) when app is in BACKGROUND (this is a big problem).

I write the code (access to a Web Service) in starter service, and with B4A 8.3+ the Starte Service are ignored and not destroyed (perfect), but.
How avoid to destroy other serve and especially httputils2service

i try to do a StartService( HttpUtils2Service)

but, this is the trace crack in my app

** Service (httputils2service) Start **
** Service (starter) Destroy (ignored)**
** Service (httputils2service) Destroy **
Solicitud a WS Enviada
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=nts.gpstoy/anywheresoftware.b4a.samples.httputils2.httputils2service }: app is in background uid UidRecord{11a349d u0a189 LAST bg:+1m2s807ms idle change:cached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
at android.app.ContextImpl.startService(ContextImpl.java:1477)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at anywheresoftware.b4a.keywords.Common$9.run(Common.java:870)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6590)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 

Erel

Administrator
Staff member
Licensed User
You need to provide more information. How are you starting the job while your app is in the background?
 

netsistemas

Member
Licensed User
There are only one serive: STARTER (i try to put in other service, but are destroyed).

How to do a start a service and no destroy? that is the cuestin

(i send you full code)


B4X:
Sub Service_Start (StartingIntent As Intent)
    Try
            InitGPS
            
                'StartServiceAt(Me, DateTime.Now + (60 * 1000 * Minutos)  ,False)
            ToastMessageShow("GPStoy Service GO",False)
            
        
    Catch
        ToastMessageShow("GPStoy Service Start " & LastException.Message ,True)
        Log(LastException)
    End Try

    'nov 2018
    'Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
    
End Sub



Sub InitGPS
    Try
       
        If GPS1.GPSEnabled = False Then
            ToastMessageShow("Active la localizacion GPS", False)
            StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
        Else
            Log("GPS Iniciado")
            'el servicio solo se inicia 1 vez... el resto lo hace el gps1
            GPS1.Stop
           
            If modVarios.V_Config.ServicioActivo = "S" Then
                modVarios.V_Config.HoraActiva = True 
                GPS1.Start(  modVarios.V_Config.Intervalo * 1000  , 0) 'Listen to GPS with no filters.
               
            Else
                modVarios.V_Config.HoraActiva = False 
                GPS1 = Null
                StopService(Me)
            ToastMessageShow("GPStoy Servicio OFF en servicio", False)
            End If 
        End If
    Catch
        'Msgbox (LastException.Message,"ERROR EN INITGPS")
        Log(LastException)
    End Try
End Sub
 

Erel

Administrator
Staff member
Licensed User
You need to add another service and make it a foreground service if you want your app to run in the background.
 

netsistemas

Member
Licensed User
I can't understand how to do a Foreground service.
Maybe i no understand the concept between Service VS Activity and Foreground and background
 

Erel

Administrator
Staff member
Licensed User
Create another service and call Service.StartForeground in that service.
 
Top