【问题标题】:How to stop custom android service?如何停止自定义android服务?
【发布时间】:2014-07-22 03:35:38
【问题描述】:

我有一个服务:

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

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

在一种环境中开始

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

如何从其他地方停止此服务?

我尝试过这样做:

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

但它并不总是有效

【问题讨论】:

  • 在什么情况下它似乎不起作用?
  • 随机大小写。它的背景 gps 服务。有时会停止,有时不会

标签: android service


【解决方案1】:

这些可能的重复可能会解决您的问题:

android LocationListener not stopping GPS use

android how to stop gps

听起来最重要的是确保没有任何东西仍在请求位置更新。

【讨论】:

    【解决方案2】:

    答案就在这里:

    Stop Location Listener in Android

    在我的例子中,它的调用方法 StopLocationUpdates

    [Service]
    public class LocationService : Service, ILocationListener
    {
    ....
    
    public override StartCommandResult OnStartCommand(...)
    { 
         return StartCommandResult.Sticky;
    } 
    
    public void StopLocationUpdates()
    {
        LocManager.RemoveUpdates(this);
        LocManager= null;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-28
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多