I have the following service where I want to detect when the phone goes from WiFi to Mobile and vice versa. Sub PE_ConnectivityChanged gets called only at startup of the service. When testing I turn off WiFi but it never goes to this sub. Do I need to do something else either in my code or in my testing to get this to trigger?
In B4a Log:
ConnectivityChanged: WIFI, state = CONNECTED
Thanks for any help with this question,
Greg
In B4a Log:
ConnectivityChanged: WIFI, state = CONNECTED
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Private nid As Int = 1
Public GPS1 As GPS
Private Tracking As Boolean
Private lock As PhoneWakeState
Dim PE As PhoneEvents
Dim PhoneId As PhoneId
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
GPS1.Initialize("gps")
If GPS1.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
End If
' Determine when there is a network change
PE.InitializeWithPhoneState("PE", PhoneId)
' These keeps battery usage down
lock.PartialLock
lock.KeepAlive(False)
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification)
Track
End Sub
Public Sub Track
If Tracking Then Return
GPS1.Start(0,0) 'Listen to GPS with no filters
Tracking = True
End Sub
Sub GPS_LocationChanged (Location1 As Location)
' Determine if in the process of loading the List or comparing GPS coordinates
If Main.bLoadingCLV Or Main.bComparing Then Return
CallSub2(Main, "LocationChanged", Location1)
End Sub
Sub CreateNotification As Notification
Dim n As NB6
Dim cs As CSBuilder
Dim bmpSign As Bitmap = LoadBitmapResize(File.DirAssets, "sign.png", 24dip, 24dip, False)
Dim title As Object = cs.Initialize.Color(Colors.Red).Append("Caution:").PopAll
Dim Content As Object
Content = cs.Initialize.Append("This app may drain your battery!").PopAll
Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "Small Cartoon.png", 256dip, 256dip, True)
n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(bmpSign)
n.LargeIcon(largeIcon)
Return n.Build(title, Content, "tag", Me)
End Sub
Sub PE_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
Log("ConnectivityChanged: " & NetworkType & ", state = " & State)
End Sub
Sub Service_Destroy
If Tracking Then
GPS1.Stop
End If
Tracking = False
lock.ReleaseKeepAlive
lock.ReleasePartialLock
End Sub
Thanks for any help with this question,
Greg