Android Question HttpUtils2Service starts at a random time depending on devices

Status
Not open for further replies.

vecino

Well-Known Member
Licensed User
Longtime User
Hi, after a long thread and a lot of testing, we came to the conclusion that when running this:
B4X:
StartReceiver(HttpUtils2Service)
Depending on the device it may start instantly, or I may start in a few seconds, or it may start in minutes, or it may never start.

This is a problem because in the meantime nothing that uses "http" works.
Only when "HttpUtils2Service" is already started then any http query works fine.

Tests have been done on many devices, and the problem is random, on most of them it works fine. But there are others where it does not start "HttpUtils2Service" right away, as I explained before.

If you try to do, for example, something like "HttpJob.DownLoad" or "HttpJob.j.GetString", etc. you get the message:
sending message to waiting queue of uninitialized activity (submitjob)

It is a very misleading message and apparently has nothing to do with the problem.

Attached is a test project, surely on most devices it will work fine.
The problem is:
How to solve it on devices that take time to start "HttpUtils2Service"?

Here a log of a device:
*** mainpage: B4XPage_Created
TempFolder=
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
*** Receiver (httputils2service) Receive (first time) ***
TempFolder=/data/user/0/b4a.example/cache
0.001 s (Count: 1)


Here a log from another device:
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
TempFolder=
*** Receiver (httputils2service) Receive (first time) ***
TempFolder=/data/user/0/b4a.example/cache
0.965 s (Count: 427)
{"id":1,"title":"iPhone 9","description":"An apple mobile which is nothing like

Thank you very much.
 

Attachments

  • ProblemHTTP.zip
    10.3 KB · Views: 215

sirjo66

Well-Known Member
Licensed User
Longtime User
in my Samsung S10e with Android 12:

Immagine.png
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Call this sub at the beginning of your app:
B4X:
Private Sub StartHttpUtils2Service
    #if release
    If HttpUtils2Service.TempFolder = "" Then
        Dim jo As JavaObject
        jo.InitializeNewInstance(Application.PackageName & ".httputils2service", Null)
        jo.RunMethod("onReceive", Array(Null, Null))
    End If
    #end if
End Sub
 
Upvote 1

marcick

Well-Known Member
Licensed User
Longtime User
Yes, I'm working on a future release with B4XPages but it takes time. For now I'll put it in the starter service as you suggest. Thank you ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Important to write that this issue will not be resolved simply by switching to B4XPages. It looks like on some devices the OS doesn't let receivers start right after boot. Assuming that this works properly without side effects, I will implement it directly in OkHttpUtils2.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
(Best to use B4XPages)

In a non-B4XPages project I recommend putting it in Service_Create of the starter service.
Is there any drawback in using Service_Create even in B4XPages projects?
I'd prefer a single solution regardless of project type.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
Call this sub at the beginning of your app:
B4X:
Private Sub StartHttpUtils2Service
    #if release
    If HttpUtils2Service.TempFolder = "" Then
        Dim jo As JavaObject
        jo.InitializeNewInstance(Application.PackageName & ".httputils2service", Null)
        jo.RunMethod("onReceive", Array(Null, Null))
    End If
    #end if
End Sub
Hi, I have done several tests and so far they have all worked well.
It starts "httputils2service" immediately, without delay.
I think the problem is solved.
Thank you very much.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Call this sub at the beginning of your app:
For a B4xPages project which of the following should be considered the "beginning of the app" ?
- Starter.Service_Create
- Main.Activity_Create (B4A) ** Main.Application_Start B4i) ** Main.AppStart (B4J)
- B4xMainPage.Initialize
- B4xMainPage.B4XPage_Created

And what if the app has one or more Receivers (e.g. for FCM)?

I expect the Main.xxx trio to be the preferred point where to call that code, but receivers/services may change that.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
For a B4xPages project which of the following should be considered the "beginning of the app" ?
- Starter.Service_Create
- Main.Activity_Create (B4A) ** Main.Application_Start B4i) ** Main.AppStart (B4J)
- B4xMainPage.Initialize
- B4xMainPage.B4XPage_Created

And what if the app has one or more Receivers (e.g. for FCM)?

I expect the Main.xxx trio to be the preferred point where to call that code, but receivers/services may change that.
I would say that starter service exists only for Android projects, so for a true B4X projects, it should be ruled out.
The MainPage option is available for all 3 platforms, so generic code should go there.
The Main.xxx trio is best suited for platform specific code.
Just my 2 cents...
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I have put it here, and so far it is working well.
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   

    StartHttpUtils2Service           

End Sub
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
This issue occurs only in b4a. So it’s logical to deal with this in b4a starter or main module.
 
Upvote 0

vecino

Well-Known Member
Licensed User
Longtime User
I don't quite understand what you are saying.
In the program with "Activity" I am using it this way and it also works fine:

B4X:
Sub Activity_Create(FirstTime As Boolean)

    StartHttpUtils2Service

    Activity.LoadLayout("frmMain")
 
Upvote 0

nedium

Active Member
Licensed User
Longtime User
HI
I have used this code but when starting the application in some cases it gives the error, I was able to do it in the starter service but still, I don't know if I have skipped a step or if the phone model has something to do with it, it is a Samsung A72
 
Upvote 0
Status
Not open for further replies.
Top