Android Question Checking internet connection

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
In 2024 What is the best way to check internet connection (wifi, 4G) when application starts.

Thanks you
 

Sergio Haurat

Active Member
Licensed User
Longtime User
In B4XPage, I found these two ways to detect if there is an Internet connection. The problem, both are only for Android. "PhoneEvents" does not exist in the internal iPhone library or possibly I did not find it

Easy way:
Sub Class_Globals
    Private pEvents As PhoneEvents
End Sub

Public Sub Initialize
  pEvents.Initialize("PhoneStatus")
End Sub

Private Sub PhoneStatus_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    If State = "CONNECTED" Then
        'Code
    Else If State <> "CONNECTED" Then
        'Code
    End If
End Sub

This library

 
Upvote 0

PoppaBart

Member
In B4XPage, I found these two ways to detect if there is an Internet connection. The problem, both are only for Android. "PhoneEvents" does not exist in the internal iPhone library or possibly I did not find it

Easy way:
Sub Class_Globals
    Private pEvents As PhoneEvents
End Sub

Public Sub Initialize
  pEvents.Initialize("PhoneStatus")
End Sub

Private Sub PhoneStatus_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    If State = "CONNECTED" Then
        'Code
    Else If State <> "CONNECTED" Then
        'Code
    End If
End Sub

This library

Try WaitForInternet

 
Upvote 0

Sergio Haurat

Active Member
Licensed User
Longtime User
In many cases the best approach is simply to try to connect to the server you need and if it fails then show a message to the user.

Thanks for your comment, I understand that you are referring to a "try/catch" using, for example, this code: https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/

I found ping on iPhone. To test it, I would do it to Google's DNS 8.8.8.8. I didn't see Ping example on Android.


You don't always need to connect to a server.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
In many cases the best approach is simply to try to connect to the server you need and if it fails then show a message to the user.
Other alternatives which I use in my apps.
which has the possibility of checking other ports.
Lib: Network, JNetwork y iNetwork
Test in B4A and B4J
B4X:
    Wait For (Ping("google.com", 80)) Complete (Status As Boolean)
    Log(Status)
   
    Wait For (Ping("172.66.43.61", 80)) Complete (Status As Boolean) 'cnn.com
    Log(Status)
   
    Wait For (Ping("cnn.com", 80)) Complete (Status As Boolean) 'cnn.com
    Log(Status)
   
    Wait For (Ping("localhost", 80)) Complete (Status As Boolean)
    Log(Status)

B4X:
Public Sub Ping(Host As String, Port As Int) As ResumableSub
    Dim Socket As Socket
    Socket.Initialize("Socket")
    Socket.Connect(Host, Port, 60 * DateTime.TicksPerSecond)
    Wait For Socket_Connected (Successful As Boolean)
    Return Successful
End Sub
 
Upvote 0
Top