Android Question GPS start/stop problem

Dario126

Member
Licensed User
Longtime User
I have problem with stopping GPS sometimes. If I use it in activity then start & stop works ok, but I had to separate it between activity and service like this:

B4X:
ACTIVITY:

Sub Activity_Create(FirstTime As Boolean)
    StartService(Service1)
End Sub



SERVICE1:

Sub Service_Create
    'PUT SERVICE IN FOREGROUND STATE - SO THAT SERVICE WILL NOT BE KILLED BY OS
    Log(" put service to foreground")
    Dim sNotif As Notification
    sNotif.Initialize
    sNotif.Icon = "icon"  ' empty string will cancel notification ...
    sNotif.OnGoingEvent = True
    sNotif.SetInfo("PD","Service Running",Main)
    sNotif.Sound  = False
    sNotif.Vibrate = False
    sNotif.Notify(1)
    Service.StartForeground(1,sNotif)
End Sub


Sub Service_Start (StartingIntent As Intent)
    'ENABLE GPS
    If GPS1.GPSEnabled = False Then
    ToastMessageShow("Please enable the GPS device", True)
        StartActivity(GPS1.LocationSettingsIntent)  
    Else
    GPS1.Start(1000,0) 'Listen to GPS with no filters
    End If
End Sub


Sub Service_Destroy
    GPS1.Stop
    Log(" GPS1.Stop")
End Sub


GPS is started normaly, and after app closes GPS1.Stop is called, but GPS sensor thus not stop working (GPS icon in notification bar still working). What is wrong here?
 

DonManfred

Expert
Licensed User
Longtime User
The GPS-icon in notificationbar you cannot en-/disable by code. Thats why you must call

B4X:
StartActivity(GPS1.LocationSettingsIntent)

to let the user set it enabled. GPS1.Stop stop working to get GPS-Updates from the GPS-Hardware but the GPS-Icon still remains.
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
The GPS-icon in notificationbar you cannot en-/disable by code. Thats why you must call

B4X:
StartActivity(GPS1.LocationSettingsIntent)

to let the user set it enabled.
GPS1.Stop stop working to get GPS-Updates from the GPS-Hardware but the GPS-Icon still remains.

I understand that, but on one simple app that I have made before, when I called GPS.stop then icon from notification bar has disappeared, and I presume that GPS sensor also has stop working? Next time when I call GPS.Start, GPS notification icon appears and I start to gain info from sensor. No need to go GPS.LocationSettingsIntent again if enabled by user before. So, just gps.start and gps.stop was enough to start and stop notification icon (and sensor?).

Now I again tried that same app, and it happens that sometimes GPS.stop thus shutdown GPS notification icon (and sensor?), and sometimes not .. Is this device dependent (device is Huawei P6), or operating system makes decision when to stop sensor/notification or not?
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Did this get resolved? I have the same issue. When I run the GPS example program when it closes the icon goes away. When I run the GPS as a service in my app the icon remains even after I Stop GPS1 in the Service_Destroy event. All I want to do is get the speed periodically (every 1-5 minutes). After that I want it gone completely. I am using a seperate service to start the GPS service. Thanks in advance for any help.
 
Last edited:
Upvote 0
Top