我的 Gps 服务出现问题

Issue in my Gps service

自 2 或 3 周以来,我一直被一个问题困住,在网上搜索每个 tuto 或相同的问题,但我没有找到问题的答案。

我实际上在开发一个 Android 应用程序 运行 Xamarin in C#

这是我的问题: 我想在我的应用程序开始时启动一个 GPS 服务,每 X 分钟请求一次坐标(这不相关)

所以我的服务看起来像:

[Service(Name = "com.xamarin.ServicesDemo2")]
public class GpsService : Service, ILocationListener
{
    static readonly string TAG = typeof(GpsService).FullName;

    private LocationManager _locationManager;
    private Location _location;

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        Log.Debug(TAG, "OnStartCommand");


        if (_locationManager == null)
            _locationManager = (LocationManager)GetSystemService(LocationService);

        _locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 0, 0, this);

        return StartCommandResult.Sticky;
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnDestroy()
    {
        Log.Debug(TAG, "OnDestroy");
        base.OnDestroy();
    }

    public void OnLocationChanged(Location location)
    {
        _location = location;
    }

    public void OnProviderDisabled(string provider)
    {
    }

    public void OnProviderEnabled(string provider)
    {
    }

    public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
    {
    }
}

我在我的 mainActivity 中这样调用它:

private void BtnValidateClick(object sender, EventArgs e)
{
    Intent serviceToStart = new Intent(this, typeof(GpsService));
    StartService(serviceToStart);
}

当我 运行 这样做并在 OnLocationChanged 方法中放置一个断点时,我永远不会进入那里,所以我想我做错了什么。

这是我的问题:

最后,我想每 X 分钟触发一次的原因是不要过度使用设备的电池,所以如果您有其他方法,我会采用!

我为我的英语道歉,我尽力了...

提前致谢!

我在模拟器上 运行 解决了这个问题!当我在模拟器上发送位置时,OnLocationChanged 被触发,这就是我所需要的。