Android Question Check internet connection and permissions

asales

Expert
Licensed User
Longtime User
I use this code to check if there is an internet connection:
B4X:
Sub CheckConnection As Boolean
    Dim p As Phone
  
    If (p.GetDataState == "CONNECTED") Then
        Return True
    End If
      
    If (p.GetSettings ("wifi_on") == 1) Then
        Return True
    End If
  
    If (p.GetDataState == "DISCONNECTED") Then
        Return False
    End If
  
    If (p.GetDataState == "SUSPENDED") Then
        Return False
    End If
End Sub

Because of the GetDataState and GetSettings of the phone library, I think I need to use the dangerous permission READ_PHONE_STATE and get permission.

I found this other function (that uses non dangerous permission ACCESS_NETWORK_STATE):
B4X:
Sub CheckConnection As Boolean
   Dim res As Boolean = False
  
   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
       res = r.RunMethod("isConnectedOrConnecting")
   End If
  
   Return res
End Sub

My question is:
to avoid get the permission to check internet connection with the phone library, the second function is the best option?
 

Semen Matusovskiy

Well-Known Member
Licensed User
Don't know about previous peleases,but in B4A 8.50 there are no permissions at all, if to include CheckConnection (first variant) only.

You can check this fact in IDE (list of permissions) and in Objects\AndroidManifest.xml.
Search in another place.
 
Upvote 0
Top