Android Code Snippet Internet / Website wait for connection and keep trying X times

Attempt to connect to a website or internet and wait/keep trying until sucessful:
#Region  Project Attributes
    #ApplicationLabel: Wait4Internet
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Scottie -  wait4internet code
'I needed a way to Attempt to connect to a website or internet
'and WAIT until I suscessfully connect or 20 tries
'the 'wait for'
'resumable will continue exectution and ALSO wait for jobdone each time its called.
'thats why I use a timer to check
'this code uses a timer to check every 2 seconds and 4 connection attempts
'will finally give up in 20 attempts.
'in designer create:  edittext1(multi line), button1, button2
'remember to add to manifest if you plan to use cleartext
' SetApplicationAttribute(android:usesCleartextTraffic, "true")
'update:  this code was for a program that runs once and closes.
'                  Timer.Initialize("Timer",2000)  would normally not be in a button


Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
      Dim Timer As Timer  
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private EditText1 As EditText   'for log
    Dim Connected As Boolean
    Dim Attempts As Int              '# of attempts to try
    Dim URL As String = ""  'website to connect/test
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Timer.Initialize("Timer",2000) '2 seconds
    Button1_Click 'at startup go ahead and test internet
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub add2log(a As String)
    EditText1.Text=EditText1.Text & a & CRLF
    EditText1.SelectionStart = EditText1.Text.Length 'scroll to end of textbox
End Sub

'this resumable will continue exectution and ALSO wait for jobdone each time its called.
' thats why I use a timer to check
Sub TestMyServer
    Dim job1 As HttpJob
    Dim a As String

    add2log("TMS: Connecting......")
    job1.Initialize("Job1", Me)
    a = job1.Download (URL) 'website to connect to / test internet connection

    Wait For (job1) JobDone(job1 As HttpJob)
    If job1.Success Then
        add2log("TMS: Connected: " & a)
        If a <> "" Then Connected=True 'if a isn't blank then I know we connected and internet works
        'if you never connect this section will show up at the end 4 times
    End If

End Sub

Sub Timer_Tick   'every 2 seconds

If Attempts=5 Or Attempts=10 Or Attempts=15 Then 'need to re connect to fully test
End If

If Attempts >= 20 Then 'try 20 times to connect
    add2log(Attempts & ": I gave up. couldn't connect")
End If

If Connected=False Then
    add2log(Attempts & ": Did not connect yet")
    add2log(Attempts & ": Connected")
    Timer.Enabled=False  'turn off timer
    'sub to continue cuz we have internet  remember connected = true
End If

End Sub

Sub Button1_Click
   EditText1.Text=""    'Reset
   Connected=False        'Reset
   Attempts=0            'Reset
 '  Timer.Initialize("Timer",2000) '2 seconds
   Timer.Enabled=True                 'turn on timer
   TestMyServer                   'connect to server
End Sub

Private Sub Button2_Click
End Sub
Last edited:


B4X founder
Staff member
Licensed User
Longtime User
1. It is a big mistake to initialize the Timer each time. It will lead to multiple timers ticking in the background.
2. This code will have bad side effects if there are multiple activities. The solution is to switch to B4XPages or move the http code to the Starter service.
3. I would have done it with code similar to:
Dim Connected As Boolean = False
For Retries = 20 To 0 Step -1
Dim job1 As HttpJob
Wait For (job1) JobDone(job1 As HttpJob)
if job1.Success Then
  Connected = True
End If
If Connected Then
Log("Failed to connect")
End If
This code is simpler and "more local".


Licensed User
Longtime User
Erel, Your soo right. I should have stated this code 'was' for a run once and close app.
Never Initialize a timer over and over. ha ha. I updated the code for others.