Android Question android.net.conn.CONNECTIVITY_CHANGE Action/Intent

henrywood

Active Member
Licensed User
Longtime User
Hi again !

Before I go on to my question, I must just say that this is an amazing forum when starting out using B4A ! Kudos to all for that :)

Now my question:

How do I get the proper Extra of the retIn Object that says "Connected=True/False" from an Intent that has action=android.net.conn.CONNECTIVITY_CHANGE into a Boolean value isNetAvailable ?

My code:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim BroadCast As BroadCastReceiver
   Dim IsNetAvailable As Boolean = True
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

  BroadCast.Initialize("BroadcastReceiver")

  BroadCast.addAction("android.net.conn.CONNECTIVITY_CHANGE")
  BroadCast.SetPriority(2147483647)
  BroadCast.registerReceiver("")

End Sub

Sub BroadcastReceiver_OnReceive (Action As String, i As Object)

  Dim retIn As Intent
  retIn = i

  If Action = "android.net.conn.CONNECTIVITY_CHANGE" Then

         ' How do I get the proper Extra of the retIn Object that says "Connected=True/False" ?   
         IsNetAvailable = .... ???

  End If
 
 
   Broadcast.AbortBroadcast


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


/Henrik
 

henrywood

Active Member
Licensed User
Longtime User
Well Now I am having some success solving my problem myself:

I am now using this code:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim BroadCast As BroadCastReceiver
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim IsNetAvailable As Boolean
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")

  BroadCast.Initialize("BroadcastReceiver")

  BroadCast.addAction("android.net.conn.CONNECTIVITY_CHANGE")
  BroadCast.SetPriority(2147483647)
  BroadCast.registerReceiver("")

End Sub

Sub BroadcastReceiver_OnReceive (Action As String, i As Object)
    
   Dim retIn As Intent
     retIn = i
   Dim IsConnected As String
   Dim TheType As String
  
   If Action = "android.net.conn.CONNECTIVITY_CHANGE" Then

     Dim jo As JavaObject = retIn
     Dim NetworkInfo As JavaObject = jo.RunMethod("getParcelableExtra", Array("networkInfo"))
     TheType = NetworkInfo.RunMethod("getTypeName", Null)
     IsConnected = NetworkInfo.RunMethod("getState", Null)

     If IsConnected.EqualsIgnoreCase("CONNECTED") Then
       IsNetAvailable = True
     Else
       IsNetAvailable = False    
     End If
    
   End If

  BroadCast.AbortBroadcast

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Please feel free to test/use it.

It *should* detected changes both in mobile data networks and WIFIs



/Henrik
 
Upvote 0
Top