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

Discussion in 'Android Questions' started by henrywood, Mar 18, 2015.

  1. henrywood

    henrywood Active Member Licensed 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:

    Code:
    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
     
  2. henrywood

    henrywood Active Member Licensed User

    Well Now I am having some success solving my problem myself:

    I am now using this code:

    Code:
    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
     
    Daniel-White and FrankBerra like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice