ssb.serman
ssb.serman

Reputation: 155

How to stop custom android service?

I have a service:

[Service]
public class LocationService : Service, ILocationListener
{
....

public override StartCommandResult OnStartCommand(...)
{ return StartCommandResult.Sticky; } 
}

Started in one of environment

new Task(() =>
{
Application.Context.StartService(new Intent(Application.Context, typeof(LocationService)));
}).Start();

How to stop this service from another place?

I tried to do this:

Application.Context.StopService(new Intent(Application.Context, typeof(LocationService))); 

but it does not always work

Upvotes: 0

Views: 244

Answers (2)

ssb.serman
ssb.serman

Reputation: 155

the answer was here:

Stop Location Listener in Android

In my case its call method StopLocationUpdates

[Service]
public class LocationService : Service, ILocationListener
{
....

public override StartCommandResult OnStartCommand(...)
{ 
     return StartCommandResult.Sticky;
} 

public void StopLocationUpdates()
{
    LocManager.RemoveUpdates(this);
    LocManager= null;
}

}

Upvotes: 0

bstar55
bstar55

Reputation: 3624

These possible duplicates might solve your issue:

android LocationListener not stopping GPS use

android how to stop gps

Sounds like the big thing is making sure nothing is still requesting location updates.

Upvotes: 1

Related Questions