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?
 

pliroforikos

Active Member
Licensed User
I'm working on about 3 hours.
It is working very well but i have to change many things in my code. For now i create a function runOnce in MainPage and moved all code from B4XPage_Create and then i call them from _Appear as your example. One strange thing is that i lost AddMenuItems at the first run. When i return to mainPage from other page they are in their place.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Yes but in your example u are using Root.LoadLayout("MainPage") in your function ContinueHere
Yes, to avoid loading the layout more than once.

Usually it is placed in the B4XPage_Created for this reason but since first there is the Internet check I wanted to avoid loading the layout and then running the check but it is possible to load the layout also in the B4XPage_Created, it was just my choice.
 
Upvote 0

pliroforikos

Active Member
Licensed User
Before continue first allow me to buy you a beer or espresso maybe? I send you some money just now. Thank you for your time.

Now this is the image from dialog and the menu. But dialog can't forbid the menu to be used.


pic.jpg
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Before continue first allow me to buy you a beer or espresso maybe? I send you some money just now. Thank you for your time.
Thank you, @pliroforikos.

But dialog can't forbid the menu to be used.
The reason is that the library uses the B4XDialog, which does not cover the title and menu bar.

Now I don't know if you can disable the menu before running the Internet test. (searching :)...)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Now I don't know if you can disable the menu before running the Internet test. (searching :)...)
No, I haven't found a way to disable a menu, not even by acting directly on the Activity.

For now you can use a boolean variable (for example: Dim InternetAvailable As Boolean), check its value inside the menu click event and exit the event (immediate return) if it is False.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
No, I haven't found a way to disable a menu, not even by acting directly on the Activity.

For now you can use a boolean variable (for example: Dim InternetAvailable As Boolean), check its value inside the menu click event and exit the event (immediate return) if it is False.
Another way, not easy at all, is to use this Erel source code with which to delete the menu items but then you have to add them again.
https://www.b4x.com/android/forum/threads/titlebar-menuitem.120908/post-755911
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Class_Globals:
'
'
'
Private mInternetAvailable As Boolean

B4X:
Private Sub B4XPage_Appear
    mInternetAvailable = False
    CheckInternet.ParentView = Root
    Wait For (CheckInternet.Check(True)) Complete(Result As Boolean)
    
    Dim Toast As BCToast
    Dim Msg As String = "Internet enabled = " & Result
    Toast.Initialize(Root)
    Toast.Show(Msg)
    Log(Msg)
    
    If Result Then
        mInternetAvailable = True
        ContinueHere
    Else
        ExitApplication
    End If
End Sub

B4X:
Private Sub B4XPage_MenuClick (Tag As String)
    If Not(mInternetAvailable) Then Return
    ToastMessageShow("menu", False)
End Sub
 
Upvote 0

pliroforikos

Active Member
Licensed User
You made me open the pc again!
Interesting code. So we can bypass menus by checking if boolean mInternetAvailable is false.
Good idea. I 'll check it tomorrow.
Thank you again and goodnight.

1629664764212.png
 
Upvote 0

pliroforikos

Active Member
Licensed User
Finaly, all checks made. Application works thanks to your lib.
Menus disabled as you proposed by checking if internet active or not.
Thank you very much.
 
Upvote 0
Top