Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的Gps服务中的问题_C#_Android_Xamarin_Service_Gps - Fatal编程技术网

C# 我的Gps服务中的问题

C# 我的Gps服务中的问题,c#,android,xamarin,service,gps,C#,Android,Xamarin,Service,Gps,从现在的两三个星期以来,我一直被一个问题困扰着,在网上搜索每一个图图或同一个问题,但我没有找到我问题的答案 我实际上是在用C语言运行Xamarin的Android应用程序上工作的# 这是我的问题: 我想在应用程序开始时启动GPS服务,每X分钟请求一次坐标(不相关) 所以我的服务看起来像: [Service(Name = "com.xamarin.ServicesDemo2")] public class GpsService : Service, ILocationListener {

从现在的两三个星期以来,我一直被一个问题困扰着,在网上搜索每一个图图或同一个问题,但我没有找到我问题的答案

我实际上是在用C语言运行Xamarin的Android应用程序上工作的#

这是我的问题: 我想在应用程序开始时启动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)
    {
    }
}
在我的主要活动中,我这样称呼它:

private void BtnValidateClick(object sender, EventArgs e)
{
    Intent serviceToStart = new Intent(this, typeof(GpsService));
    StartService(serviceToStart);
}
当我运行这个程序并在
OnLocationChanged
方法中设置断点时,我永远不会进入其中,所以我想我做错了什么

以下是我的问题:

  • 如何知道何时触发了
    OnLocationChanged
  • 在代码中如何以及在何处指示我要每X分钟触发一次
    OnLocationChanged
最后一件事,我想每X分钟触发一次的原因是为了不过度使用设备的电池,所以如果你有其他方法,我就用它

我为我的英语道歉,我已经尽力了


提前谢谢

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

Hi,你们在emulator上试过吗?你可以通过发送位置来触发事件。嘿,我想关闭我的线程,因为我通过在模拟器上运行解决了这个问题!是的,当我在emulator上发送位置时,OnLocationChanged被触发,这就是我所需要的,谢谢你的回答,再见!您可以为您的问题提供答案,并对其进行标记。它会帮助别人。