0

I'm trying to use Localbroadcast to send latitude and longitude over to the main activity. But the parameter of this within getInstance() isn't working. I tried adding the context class but it is still wrong. Any ideas? Thanks.

companion object {
    val TAG = "LocationTrackingService"

    val INTERVAL = 5000.toLong() // In milliseconds
    val DISTANCE = 0.toFloat() // In meters

    val locationListeners = arrayOf(
        LTRLocationListener(LocationManager.GPS_PROVIDER),
        LTRLocationListener(LocationManager.NETWORK_PROVIDER)
    )
    private fun sendRequest() {
        // The string "GPS_not_activaded" will be used to filer the intent
        val intentRequest = Intent("Sending_location_coordinates")
        LocalBroadcastManager.getInstance(this).sendBroadcast(intentRequest)
        Log.d(TAG,"Se enviaron las coordenadas")
    }

    class LTRLocationListener(provider: String) : android.location.LocationListener {

        val lastLocation = Location(provider)


        override fun onLocationChanged(location: Location?) {
            lastLocation.set(location)
            Log.d(TAG, "======================= New Data =======================")
            Log.d(TAG,"latitud: "+ lastLocation.latitude)
            Log.d(TAG,"longitud: "+ lastLocation.longitude)
            sendRequest()
        }
gidds
  • 16,558
  • 2
  • 19
  • 26
Javiseeker
  • 47
  • 1
  • 11
  • why are you nesting all of this in a companion object? `this` refers to the Companion itself, which is not a subclass of Context – Tim Feb 27 '19 at 14:41
  • I nested the companion for extra variables for the service, I don't understand really well the context, should i put all of the data outside the companion? – Javiseeker Feb 27 '19 at 14:49

1 Answers1

0

Try

 this@LocationTrackingService

to get LocationTrackingService class this object.

More about this object here.

Sdghasemi
  • 5,370
  • 1
  • 34
  • 42