Android Question Wait for Internet Connection

pliroforikos

Active Member
Licensed User
Hello,

I am using the bellow function to test internet connection and works ok

B4X:
Public Sub testInternet(url as String) As ResumableSub
    Dim result As Boolean = False
    Dim Job As HttpJob
        Job.Initialize("Job", Me)
        Job.Download(url)
        Log ("Sending job")
        Wait For(Job) JobDone(Job As HttpJob)
        If Job.Success Then
            result = True
            Log("Success")
        Else
            result = False
            Log("No connection...")
        End If
        Job.Release
    Return result
End Sub

But now i want app not to do anything until Internet will be available so i did this


B4XMainPage:
    Dim rst As Boolean = False
    Do While rst = False
        Wait for(testInternet("the url i want to test e.g. google.com")) Complete (rst As Boolean)
    Loop

When true it continues but when internet not available it does some work for a few tries but without waiting success continues the app and then the app crash.

Any idea?
 

LucaMs

Expert
Licensed User
Longtime User
Hello,

I am using the bellow function to test internet connection and works ok

B4X:
Public Sub testInternet(url as String) As ResumableSub
    Dim result As Boolean = False
    Dim Job As HttpJob
        Job.Initialize("Job", Me)
        Job.Download(url)
        Log ("Sending job")
        Wait For(Job) JobDone(Job As HttpJob)
        If Job.Success Then
            result = True
            Log("Success")
        Else
            result = False
            Log("No connection...")
        End If
        Job.Release
    Return result
End Sub

But now i want app not to do anything until Internet will be available so i did this


B4XMainPage:
    Dim rst As Boolean = False
    Do While rst = False
        Wait for(testInternet("the url i want to test e.g. google.com")) Complete (rst As Boolean)
    Loop

When true it continues but when internet not available it does some work for a few tries but without waiting success continues the app and then the app crash.

Any idea?
The app will continue the work you wrote before the Do While loop and this is how it should work, that's correct.
Only the code written after the Do While will be executed when there will be an Internet connection.
It is up to you to move the part of code that needs the connection after the Do While.
 
Upvote 0

pliroforikos

Active Member
Licensed User
The do while loop is the first code in the mainPage_Created
But it continues with code after do while and in _appear without receiving true
 
Upvote 0

emexes

Expert
Licensed User
But it continues with code after do while and in _appear without receiving true
I'm pretty sure that's what ResumableSubs do - the first time they hit a Sleep or Wait, execution forks: one path Returns immediately, the other pauses until the condition is met and then continues from the next line. Confusing as $#@! first time you discover it, but often useful once tamed.

I will cheerfully delete this post if I am wrong and/or anybody else has better information. 🍻
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm pretty sure that's what ResumableSubs do - the first time they hit a Sleep or Wait, execution forks: one thread Returns immediately, the other pauses until the condition is met and then continues from the next line. Confusing as $#@! first time you discover it, but often useful once tamed.
Not exactly but just because there won't be more than one thread, the rest is correct.

So at the first Wait For (in that case) the execution continues in the B4XPage_Appear, if it exists.
 
Upvote 0

emexes

Expert
Licensed User
If you keep ending all your posts with a beer, someone will become an alcoholic 😄
I tried using carrots instead of beer but that only worked with asse donkeys so would be ineffective with you. Whereas beer seems to work everytime. 🍻
 
Upvote 0

pliroforikos

Active Member
Licensed User
I'm pretty sure that's what ResumableSubs do - the first time they hit a Sleep or Wait, execution forks: one path Returns immediately, the other pauses until the condition is met and then continues from the next line. Confusing as $#@! first time you discover it, but often useful once tamed.

I will cheerfully delete this post if I am wrong and/or anybody else has better information. 🍻
I think you are right but thats why i used the while loop without success ...
 
Upvote 0

pliroforikos

Active Member
Licensed User
I attached a project where the B4XPage_Appear continues before Wait For complete

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Log($"B4XPage Created STARTS here"$)
    Wait for(testInternet) Complete (Rtrn As Boolean)
    Root.LoadLayout("MainPage")
    
    If Rtrn Then
        Log("Internet Ready")
    Else
        Log("Internet Not Ready")
    End If
    Log($"B4XPage Created ENDS here ..."$)
End Sub
The log shows:
[QUOTE]
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
B4XPage Created STARTS here
Sending job
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
B4XPage_Appear STARTS here ...
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Success
Internet Ready
B4XPage Created ENDS here ...

[/QUOTE]
Private Sub B4XPage_Appear ()
    Log("B4XPage_Appear STARTS here ...")
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub


Public Sub testInternet() As ResumableSub
    Dim result As Boolean = False
    Dim Job As HttpJob
    Job.Initialize("Job", Me)
    Job.Download("https://www.google.com" )
    Log ("Sending job")
    Wait For(Job) JobDone(Job As HttpJob)
    If Job.Success Then
        result = True
        Log("Success")
    Else
        result = False
        Log("No connection...")
    End If
    Job.Release
    Return result
End Sub

So Wait For() only pause current function B4XPage_create and not all program execution. Any idea how to pause all program?
 

Attachments

  • Project.zip
    14.4 KB · Views: 119
Upvote 0

pliroforikos

Active Member
Licensed User
:)
Sorry, i had to test your solution in my real project.
So am greatfull for your time and thank you very much.

You moved all b4xPage_Create code to b4xPage_Appear and it is working on this small project but:

I want to use this code in several situations and more times in every _Page especialy when i want to call data from external database.

On the other hand you gave me some ideas to work on so i need time to think and code.

P.S. I usualy check the forum or stay in for a lot of time in the day. It's the best a place to learn B4X, maybe help others and maybe spend some creative time.
I only downloaded your code once!

  1. Yes, @LucaMs, it works (thank you?)
  2. No, @LucaMs, it does not work for me (thank you anyway?)
  3. Mind your own business, @LucaMs
Answer 1 and 2 with thank you.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm thinking there are two possible solutions, both of which are valid:
  1. create a B4XPage that checks if Internet is active.
  2. create a Custom View.
Any page could "show" it (if necessary, since being a class it can be used (instance-object) even without viewing the GUI-Root).

The CustomView should instead be in any basic page layout where the check is necessary.

P.S.
3. also as B4XLib it would be useful.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If you are not concerned about cross platform (or don’t mind writing platform specific code), for Android, you could do the check in Main’s Activity_Resume and only delegate the resume call if you have an internet connection or if an internet connection is not required for the active page. This way you write the code once instead off adding it to all the pages
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
If you are not concerned about cross platform (or don’t mind writing platform specific code), for Android, you could do the check in Main’s Activity_Resume and only delegate the resume call if you have an internet connection or if an internet connection is not required for the active page. This way you write the code once instead off adding it to all the pages
That is also a good solution.

However, you may need to check before running a certain project line, although the check has already been done in the Resume event.

I think a B4XLib would be useful, with a Check (or Test) method to which you can pass parameters by which you can:

1 - say not to display graphics (Internet test only)
2 - force the user to activate an Internet connection or abort the operation, with GUI (in which case you would close the app).
 
Upvote 0

pliroforikos

Active Member
Licensed User
Thank you both.
I'll try the solution with B4XPage and maybe later with B4XLib. I don't know how to work with Activity_Resume as i am B4XPage kid (even 49yo)!
 
Upvote 0
Top