Android Question How to check if there is no internet B4a

Makumbi

Well-Known Member
Licensed User
How can i check using B4a for existence of internet because current iam using data on my phone to connect to the internet i wanted a function which can check whether there is no internet despite the fact that it is still connected

iam currently using this but even when i run out internet it does not show please help
B4X:
Sub IsConnectedToInternet 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
 

ac9ts

Active Member
Licensed User
Longtime User
This is what I use. It's old and there's probably a better way but it still works (I believe).

B4X:
private Sub CheckNetConnections As Boolean

    ' This sub will determine the WiFi and network connection. It will also check the 
    ' WiFi only flag and determine if the stream/download can proceed.
   
    Dim Server As ServerSocket 
    Dim WiFi_IP As String
    Dim Network_IP As String
    Dim RtnVal As Boolean
   
    Server.Initialize(21341,"Net")
    Network_IP = Server.GetMyIP
    WiFi_IP = Server.GetMyWifiIP
    Server.Close

    RtnVal = True
   
    If Common.WiFi_Only And WiFi_IP = "127.0.0.1" Then 
        RtnVal = False
        ToastMessageShow("No WiFi connection", True)
    End If
   
    If WiFi_IP = "127.0.0.1" And Network_IP = "127.0.0.1" And Not(Common.WiFi_Only) Then
        RtnVal = False
        ToastMessageShow("No Internet connection", True)
    End If
   
    Return RtnVal
   
End Sub
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i used phone events.

starter service:
B4X:
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=7.8
@EndOfDesignText@
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

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

    Public PhoneEvent1 As PhoneEvents

End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.


    'https://www.b4x.com/android/help/phone.html#phoneevents
    PhoneEvent1.Initialize("PhoneEvent")

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub PhoneEvent_ConnectivityChanged(NetworkType As String, State As String, Intent As Intent)
    
    'NetworkType - WIFI or MOBILE.
    'State - One of the following values: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN.
    
    If State = "CONNECTED" Then
    Else
        ToastMessageShow("Keine Netzwerkverbindung",True)
        Log(NetworkType)
        Log(State)
        
        'umleiten auf anderes Activity?
        
    End If
    
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…