Android Question Use Wait for JobeDone for check net

devmobile

Active Member
Licensed User
Hi
I use Waiting For JobDone for check internet status
Acctually i download file and if jobdone is successfully so intertnet is active and not downloaded so internet is disactive
And set timeout for download
Is it good way for check internet?
 

ilan

Expert
Licensed User
Longtime User
Hi
I use Waiting For JobDone for check internet status
Acctually i download file and if jobdone is successfully so intertnet is active and not downloaded so internet is disactive
And set timeout for download
Is it good way for check internet?

Yuu could also use a webview and load google.com if succeful then phone is connected if not then not...

There are less chances that google.com will fail then a file in your server :D
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
WebView is not needed here.

B4X:
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.google.com") 'or better the link that you want to access
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     'there is an internet connection
   End If
   j.Release


if b4i i can do something like this:

B4X:
    Dim webview1 As WebView
    webview1.Initialize("webview1")
    webview1.LoadUrl("http://www.google.com")


Sub webview1_PageFinished (Success As Boolean, Url As String)
    If Success Then
        Log("there is internet connection")
    Else
        Log("No internet connection)  
    End If
End Sub

but in b4a i have no Success (Boolean) returned on PageFinished Event, is there a specific reason why?

EDIT: btw your solution is good for b4a 7+ but not everyone has b4a 7+ and also no Wait For function.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I personally use MLWifi when managing my WiFi networks, I then use WiFiNetworks.isOnLine to test if I'm on the internet or not. You can also use ServerSocket and test if you current IP address is 127.0.0.1, if it is then you are not on the internet.

I don't believe in using google or any other website for that fact just to see if you are online when there are plenty of tools/libraries for B4A to do that for you :)
 
Upvote 0

devmobile

Active Member
Licensed User
I personally use MLWifi when managing my WiFi networks, I then use WiFiNetworks.isOnLine to test if I'm on the internet or not. You can also use ServerSocket and test if you current IP address is 127.0.0.1, if it is then you are not on the internet.

I don't believe in using google or any other website for that fact just to see if you are online when there are plenty of tools/libraries for B4A to do that for you :)
Yes i know but if i conenct to wifi and there isn't internet in wifi network,your idea return true (it is mistake)
 
Upvote 0

devmobile

Active Member
Licensed User
WebView is not needed here.

B4X:
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.google.com") 'or better the link that you want to access
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     'there is an internet connection
   End If
   j.Release
Ok i will use it
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but in b4a i have no Success (Boolean) returned on PageFinished Event, is there a specific reason why?
The underlying WebView doesn't report it.

EDIT: btw your solution is good for b4a 7+ but not everyone has b4a 7+ and also no Wait For function.
It should be simple to convert it to two subs instead of using Wait For.
 
Upvote 0

devmobile

Active Member
Licensed User
WebView is not needed here.

B4X:
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.google.com") 'or better the link that you want to access
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     'there is an internet connection
   End If
   j.Release
Erel tis way is good but i need to test internet connection inline not after download file
Assume when i go to 20 line,i need test connection and then continue to other line but in this way,i can detect connection after download file
How i stop it in JobDone line?
 
Upvote 0

devmobile

Active Member
Licensed User
Hard to answer your question. You need to understand what the code does and decide whether it is suitable for your requirements or not.
Thanks
I can download file with java code inline b4a without use JobDone
With below code (Java Inline in b4a)
B4X:
public boolean isServerReachable(Context context){
ConnectivityManager connMan =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMan.getActiveNetworkInfo();
if(netInfo !=null&& netInfo.isConnected()){
try{
URL urlServer =new URL("your server url");
HttpURLConnection urlConn =(HttpURLConnection) urlServer.openConnection();
urlConn.setConnectTimeout(3000);//<- 3Seconds Timeout
urlConn.connect();
if(urlConn.getResponseCode()==200){
return true;
}
else{
returnfalse;
}
}
catch(MalformedURLException e1){
returnfalse;
}
catch(IOException e){
returnfalse;}
}
returnfalse;
}
I can use top code java inline in b4a inline
Is it?
 
Upvote 0
Top