Android Question [CLOSED] removeNmeaListener does not seem to stop raising events

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

I have the following code in a service called svc_gps. Everything works well except the StopGps method that removes the listener. It just keeps raising events after the listener is removed and I added in the reset code when I deteced this on an Android 11 device which did not solve the issue at all.

B4X:
Public Sub StartGps
 
    log($"svc_gps::StartGps"$)
    Try
        mGPS.Start(0, 0)
        If phone.SdkVersion >= 24 Then
            LocationManager = context.RunMethod("getSystemService", Array("location"))
            nmea.InitializeNewInstance(Application.PackageName & ".svc_gps$MyNmeaMessageListener", Null)
            LocationManager.RunMethod("addNmeaListener", Array(nmea))
        End If
    Catch
        log("StartGps, error - " & LastException.Message)
        StopService(Me)
    End Try

End Sub

Public Sub StopGps

    Try
        mGPS.Stop
        If phone.SdkVersion >= 24 Then
            LocationManager.RunMethod("removeNmeaListener", Array(nmea))
          
            ' // 2025.09.25 - removeNmeaListener seems to fail so we will reset the LocationManager object
            LocationManager = Null
            nmea = Null          
            LocationManager = context.RunMethod("getSystemService", Array("location"))
            nmea.InitializeNewInstance(Application.PackageName & ".svc_gps$MyNmeaMessageListener", Null)
          
            log($"svc_gps::StopGps::removeNmeaListener"$)
        End If
    Catch
        log($"svc_gps::StopGps:: -error- ${LastException.Message}"$)
        finished_task
    End Try
    mod_functions.writelog($"svc_gps::StopGps::Last packet - ${starter.appset.RT_gps.rmc}"$)
  
End Sub

#if Java


public static class MyNmeaMessageListener implements android.location.OnNmeaMessageListener
{
    public void onNmeaMessage(String message, long timestamp) {
        processBA.raiseEventFromDifferentThread(null, null, 0, "nmea_event", false, new Object[] {message, timestamp});
    }
 
}

#End If

Related - https://www.b4x.com/android/forum/threads/resolved-gps-nmea-event-not-firing-on-android-10.112140/
 
Last edited:
Top