Android Question Check correct internet connection

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
I found some code for check internet connection in forum
But you consume i connect to wireless but internet is disable or not connected
In this state,again return true
it is wrong because i cannot send data
how can i detect internet state correctly?
 

ArminKH

Well-Known Member
Hi.
I found some code for check internet connection in forum
But you consume i connect to wireless but internet is disable or not connected
In this state,again return true
it is wrong because i cannot send data
how can i detect internet state correctly?
also i think you can check whether your device has ip address or not
because each device which is connected to internet has a ip addrees
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
I currently use the "official" Google recommended way to check an internet connection as explained here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

The B4A implementation using Reflection library is here:
B4X:
Sub CheckConnection As Boolean
    Dim r As Reflector
   
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
   
    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If
   
    Return False
End Sub
 
Upvote 0

ArminKH

Well-Known Member
I currently use the "official" Google recommended way to check an internet connection as explained here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

The B4A implementation using Reflection library is here:
B4X:
Sub CheckConnection As Boolean
    Dim r As Reflector
  
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
  
    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If
  
    Return False
End Sub
is this works when wifi is on but there is not any connection?
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
is this works when wifi is on but there is not any connection?
Yes. It just checks if the device has any internet connection.
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
also i think you can check whether your device has ip address or not
because each device which is connected to internet has a ip addrees
Thank you but if connect to network thus i have a valid ip example 192.168.1.1 and it is wrong
 
Upvote 0

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
I currently use the "official" Google recommended way to check an internet connection as explained here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

The B4A implementation using Reflection library is here:
B4X:
Sub CheckConnection As Boolean
    Dim r As Reflector
  
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
  
    If r.Target <> Null Then
        Return r.RunMethod("isConnectedOrConnecting")
    End If
  
    Return False
End Sub
Thank you.i try it
 
Upvote 0
Top