I have a Service, where I pass in minDistanceFilter
as extras.
class LocationService : Service() {
private var minDistanceMeters: Float = 0.00001f
override fun onStart(intent: Intent?, startId: Int) {
super.onStart(intent, startId)
val extras = intent!!.extras
minDistanceMeters = extras!!["minDistanceFilter"] as Float
}
}
This is how I start the service, and pass in the minDistanceFilter
as extras parameter.
fun startLocationUpdates() {
serviceIntent = Intent(this, locationService.javaClass)
serviceIntent.putExtra("minDistanceFilter", minDistanceFilter);
if (!Util.isBackgroundServiceRunning(locationService.javaClass, this)) {
startService(serviceIntent)
// After starting the service, we register the ServiceBroadcast as well
// to get the results back.
val inf = IntentFilter()
inf.addAction(LocationService.ActionTag)
registerReceiver(sb, inf)
Toast.makeText(this, getString(R.string.service_start_successfully), Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, getString(R.string.service_already_running), Toast.LENGTH_SHORT).show()
}
}
However when the service gets started in the background, I can see that minDistanceMeters
is still set to the default value of 0.00001f, instead of what has been passed in previously. What am I missing please?
UPDATE: This is my onStartCommand:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
return START_STICKY
}
CodePudding user response:
try this:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val extras = intent!!.extras
val item = extras.getFloat("minDistanceFilter")
return super.onStartCommand(intent, flags,startId)
}
UPDATE:
Use this:
val item = extras.getFloat("minDistanceFilter")