Android Code Snippet Check Internet Connection

Hi
Use below code for check internet connection
The 8.8.8.8 ip is special for ping and Google is owner this IP
This IP is use DNS cache for speed up ping
And never will this address stop

B4X:
Sub ping
    Dim status As Boolean
    status    =    Ping("8.8.8.8","Report",1,4)
End Sub

Sub Ping(Url As String, ResultsType As String, Attempts As Int, Timeout As Int) As Boolean

    Dim sb As StringBuilder
    Dim p As Phone
    Dim Option As String
    
    sb.Initialize

    If ResultsType = "Report" Then    Option = " -v "
    If ResultsType = "Summary" Or ResultsType = "Status" Then Option = " -q "

    p.Shell("ping -c " & Attempts & " -W " & Timeout & Option & Url, Null, sb, Null)

    If sb.Length = 0 Or sb.ToString.Contains("Unreachable") Then
        Return False
    End If
    
    Return True

End Sub
 
Top