Android Question Problem with WaitForInternet [SOLVED with other code]

jnjft

Member
Licensed User
Longtime User
Hi! I have the same problem as gregbug in this thread:
https://www.b4x.com/android/forum/t...t-way-to-check-the-internet-connection.58346/
But unfortunately using library version 1.0.2 does not fix it, like it did with gregbug.

When I start the detection of the Internet connection, the connection is correctly detected as being established or not, but immediately after that the app is crashing. I am using a Samsung Galaxy Tab II without a Sim, so mobile connection is not possible, only WLAN.

Can someone help me?
 

jnjft

Member
Licensed User
Longtime User
I have Android v. 4.2.2, API level 17

The error log goes like this:

Network WIFI connected
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x4101f930)
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in com.visualnet.waitforinternet$1@4231a2a8
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
at com.visualnet.waitforinternet$1.onReceive(waitforinternet.java:199)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
... 9 more
FATAL EXCEPTION: main
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x8000010 (has extras) } in com.visualnet.waitforinternet$1@4231a2a8
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
at com.visualnet.waitforinternet$1.onReceive(waitforinternet.java:199)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
... 9 more

Thanks for your time!
 
Upvote 0

jnjft

Member
Licensed User
Longtime User
Hi folks, don't waste your time, my problem was solved with this code from
https://www.b4x.com/android/forum/threads/testing-internet-connection.45364/#content :

B4X:
#Region Internet Connection
    Sub Test_Internet_Connection()

        Dim p As Phone
        Dim overall_data_state As Int
        Dim cellular_data_state As Int
      
        '0 Disabled - 1 Enabled. May also return a 3 in enabled state but I can find no explanation of the return values
        'Note: Turning on airplane mode is not the same as disabling wifi
        Dim wifi As Int = p.GetSettings ("wifi_on")
        'Msgbox("Wi-Fi value is: " & wifi, "")
      
        Dim airplane_mode As Int = p.GetSettings ("airplane_mode_on")
        'Msgbox("Airplane mode value is: " & airplane_mode, "")
      
        'Test the Cellular State
        Select Case p.GetDataState
            Case "CONNECTED"
                cellular_data_state = 1

            Case Else
                'Captures the other possible states of "SUSPENDED" "DISCONNECTED" "CONNECTING"
                cellular_data_state = 0
        End Select
      
      
        'Check to see if airplane_mode is enabled. 1 means device is in airplane_mode, 0 means device is not in airplane_mode
        If airplane_mode = 1 Then
            'We know for sure the internet is disconnected
            overall_data_state = 0
        Else
            'Otherwise we know that the device is not in airplane_mode so check wifi and cellular
            If wifi = 0 AND cellular_data_state = 0 Then
                'Both the cellular and WiFi are disabled so we cannot have a connection (except some kind of tethering over blue-tooth)
                overall_data_state = 0
            Else
                'Either Wifi or cellular is active so it is possible to have a connection
                overall_data_state = 1
            End If
        End If
              
            'Test the connection if it is possible to have one
            If overall_data_state = 1 Then
      
            'Initially indicate that we do have a connection and the screen label will probably be correct.
            'We do this because of the potential latency in determining when a connection is not available.
            Internet_Connected(True)
          
            'Now we will test the connection and change it back to False if it fails
            Dim job1 As HttpJob
            job1.Initialize("Job1", Me)

            'Send a GET request
            job1.Download("http://www.google.com")
          
            'Once this completes the JobDone routine is executed and the labels are set to their true state.
          
        Else
            Internet_Connected(False)
        End If
      

    End Sub

    Sub JobDone (job As HttpJob )
        If job.Success = True Then
            Internet_Connected(True)
        Else
            Internet_Connected(False)
        End If
        job.Release
    End Sub

    Sub Internet_Connected (Flag As Boolean)
        If Flag = True Then
            InternetConnected = True
            lblInternetConnected.Text = Chr(ConnectedIcon)
            lblInternetConnected.TextColor = Colors.White
        Else
            InternetConnected = False
            lblInternetConnected.text = Chr(DisconnectedIcon)
            lblInternetConnected.TextColor = Colors.Red
        End If
    End Sub
#End Region

Many thanks to Gary Milne
and Cableguy
 
Upvote 0
Top