Android Question How to check or internet connection

johnaaronrose

Active Member
Licensed User
Longtime User
I would like my app to check for an internet connection. I have only seen threads which are over a year old on this topic. I have been told that a new thread should be started when that is so.

I have just tried:
B4X:
myDevice.Initialize("")
If myPhone.GetDataState.EqualsIgnoreCase("CONNECTED") _
Or myDevice.Connected Then
      ToastMessageShow("Internet Connected", True)
Else
      ToastMessageShow("Internet Not Connected", True)
      ToastMessageShow("myPhone.GetDataState="&myPhone.GetDataState, True)
      ToastMessageShow("myDevice.Connected="&myDevice.Connected, True)
End If

But it always gives myPhone.GetDataState as "DISCONNECTED" and myDevice.Connected as False. How should this be coded? It would be nice to see the corrected coding including a test to see if the device is a phone or a tablet.
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Sub IsConn As Boolean
    Dim P As Phone
    Dim WF As ServerSocket
    Dim B As Boolean=False

    If P.GetDataState="CONNECTED" Then B=True
    If WF.GetMyIP<>"127.0.0.1" Then B=True

    Return B
End Sub

Use for this code Phone Library and Netwrok Library.
 
Last edited:
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User

I don't understand how this code tests for an internet connection: there is no IF statement to do the test. BTW httputils2 has been deprecated in favour of OkHttpUtils2.
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User

I am using both those libraries.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I don't understand how this code tests for an internet connection: there is no IF statement to do the test. BTW httputils2 has been deprecated in favour of OkHttpUtils2.
yes, you're right I forgot to write a part but the Star-Dust's solution should work for you.
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User

There doesn't seem to be an icon when posting to put a Code tag enclosing some coding. Or am I missing it?

Thanks for the tip. Unfortunately if I do that it would request a download of a file and I don't know how to process that. Star-Dust's solution worked for me once I'd coded in the missing Initialize command.
 
Last edited:
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User

A
I am using both those libraries.

Apologies, Star-Dust for implying that your method doesn't work. It works after the line
B4X:
WF.Initialize(0, "")
is inserted between the Dim and the If statements.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Initialize is not necessary in this case.
To not let you report a mistake, add the 'ignore note to the line

B4X:
Sub IsConn As Boolean 'ignore
    Dim P As Phone
    Dim WF As ServerSocket 'ignore
    Dim B As Boolean=False

    If P.GetDataState="CONNECTED" Then B=True
    If WF.GetMyWiFiIP<>"127.0.0.1" Then B=True  'ignore
    If WF.GetMyIP<>"127.0.0.1" Then B=True

    Return B
End Sub

in all my Apps this method works, I'm sorry that it does not work for you
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User

The method did work with the Initialize statement inserted. I didn't check whether it worked without the Initialize statement as I had a compiler warning stating that the WF variable was not Initialized. Due to originally programming in Fortran, Cobol & IBM 360 Assembler, I found that it was good practice to remove all compiler warnings by making code changes. It's a habit that has stuck.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…