Android Question GPS_LocationChanged not executing after starting up the GPS

rleiman

Well-Known Member
Licensed User
Hi Everyone,

This is a nice library but I'm having some trouble figuring out how to work it. In my main activity I used this coding in Activity_Create.

B4X:
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If
This code starts up the GPS.

In the starter I use this coding in GPS_LocationChanged.

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    CallSub2(Main, "LocationChanged", Location1)
End Sub
When I start my app for the first time the GPS turns on and GPS_LocationChanged inside the starter executes as expected calling "LocationChanged" in the Main activity. That in turn calls another subroutine in the Main activity which eventually calls StopGps.

After the GPS is turned off there is a call to StartGps in another part of the app and the GPS starts up. I was expecting GPS_LocationChanged to be called just like it did when I started up the app for the first time but that event never executes. I'm assuming I'm missing a step here.

Can you tell me the additional coding I need so GPS_LocationChanged can execute after the GPS is started?

Thanks.
 

MarkusR

Well-Known Member
Licensed User
the gps tutorial useing this part to StartGPS again after pause/Stop via Permission Request and if the event comes with ok it start gps again.


B4X:
Sub Activity_Resume
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = Starter.rp.PERMISSION_ACCESS_FINE_LOCATION Then
        If Result Then CallSub(Starter, "StartGPS")
    End If
End Sub
and here is a filter option, if this is not 0 the event is filtered by time or distance.
GPS1.Start(0, 0)
 

rleiman

Well-Known Member
Licensed User
Hi Markus,

I moved the coding that starts with:
If Starter.GPS1.GPSEnabled = False Then

into a subroutine called StartUpTheGPS and called it from the same place as in your sample, but it only works the first time the app is run. It won't work when calling it from:

B4X:
CallSub(Main, "StartUpTheGPS")
The above code was from another activity module.

In the main activity module I even put a toast statement and found that statement never executes if it's called from any other activity except from within the main activity.

B4X:
Public Sub StartUpTheGPS

    ToastMessageShow("StartUpTheGPS", False)

    ' Make sure there is permission to use the GPS and start it up if it's enabled.
    '------------------------------------------------------------------------------
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If   
End Sub
 

MarkusR

Well-Known Member
Licensed User
i think there is only one "activity module" active.
the log in the ide should show when activitys gone sleep or awake.
 

rleiman

Well-Known Member
Licensed User
Hi Markus,

I think there may be a bug with calling sub routines from inside a service module. I will try an experiment and let you know what happens.
 

MarkusR

Well-Known Member
Licensed User
ok, you wrote "The above code was from another activity module."
 

rleiman

Well-Known Member
Licensed User
Here's what I found.

I placed this subroutine in the Main activity module.

B4X:
public Sub Emad
    ToastMessageShow("I'm inside the Emad subroutine.", False)
End Sub
I called it from the Starter service activity like this.

B4X:
Sub Service_Start (StartingIntent As Intent)

    CallSub(Main, "Emad")

End Sub
The toast message is not displayed so it looks like I can't call a Main activity module subroutine from inside the Starter service module. I will see if there is a way around this issue.
 

rleiman

Well-Known Member
Licensed User
I just tried to copy this code into one of my service modules but it crashes the app. Looks like I also can't call anything in the Starter module from any other module except the Main activity module.

B4X:
    ' Make sure there is permission to use the GPS and start it up if it's enabled.
    '------------------------------------------------------------------------------
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If
I'm really hoping I can restart the GPS from my service module and have it generate the evens for LocationChanged.
 

MarkusR

Well-Known Member
Licensed User
the log said Activity_Create comes after Service_Start
but CallSubDelayed raise this sub from within Service_Start
 

rleiman

Well-Known Member
Licensed User
Hi Marcus,

I found that I can start the GPS with
CallSub(Starter, "StartGPS")

from another activity module and GPS_LocationChanged from the starter service module will be executed. That's where the call to Main activity module LocationChanged will be executed when the other activity module is closed using activity.finish

My problem is that I want to get all of this to happen with just doing CallSub(Starter, "StartGPS") from a service module.
 

MarkusR

Well-Known Member
Licensed User
maybe its better if you provide a example project with some remarks.
 

rleiman

Well-Known Member
Licensed User
Hi Markus,

It's a big to big to break apart but I did find out that since I had exited the app the Main activity was paused. Everything works perfectly if I'm in the Main activity which is not exactly what I need. My service module gets run at specific times of the day. I need to start up the GPS and execute some code when the GPS_LocationChanged event executes. Once it executes I will turn off the GPS to conserve the battery life.

Looks like I will need to add most of this processing into the Starter service module and call the GPS start from the service module that runs at the different times of the day. I will experiment to see if I can do that.
 

rleiman

Well-Known Member
Licensed User
Got it working. :)

I called
CallSub(Starter, "StartGPS")
from the scheduled service module which was in the Starter service module. From the Starter module I did all of the processing from GPS_LocationChanged by first stopping the GPS so that event won't be called repeatedly. This all happened when I was not viewing the app.

Wow. That was tricky. :rolleyes:
 
Top