Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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
当设备未使用Xamarin Android移动时,不会调用ILocationListener.OnLocationChanged()方法_Android_Xamarin_Xamarin.forms_Xamarin.android - Fatal编程技术网

当设备未使用Xamarin Android移动时,不会调用ILocationListener.OnLocationChanged()方法

当设备未使用Xamarin Android移动时,不会调用ILocationListener.OnLocationChanged()方法,android,xamarin,xamarin.forms,xamarin.android,Android,Xamarin,Xamarin.forms,Xamarin.android,我需要每秒捕获GPS位置,即使设备静止不动 public class GpsLocationProvider : Java.Lang.Object, IGpsLocationProvider, ILocationListener { public event EventHandler<GpsLocationReceivedEventArgs> GpsLocationReceived; private LocationManager _loc

我需要每秒捕获GPS位置,即使设备静止不动

 public class GpsLocationProvider : Java.Lang.Object, IGpsLocationProvider, ILocationListener
    {
        public event EventHandler<GpsLocationReceivedEventArgs> GpsLocationReceived;

        private LocationManager _locationManager;

        //call this method from other method to register during init.
        public void StartLocationUpdates(long minLocationUpdateIntervalInMs)
        {          
            if (_locationManager == null)
            {
                _locationManager = (LocationManager)Application.Context.GetSystemService(Context.LocationService);
            }

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

        public void StopLocationUpdates()
        {

            _locationManager?.RemoveUpdates(this);
        }

        #region ILocationListener Implementation

        public void OnLocationChanged(Location location)
        {
            if (GpsLocationReceived != null)
            {
                var gpsLocation = CreateLocation(location);

                GpsLocationReceived(this, new GpsLocationReceivedEventArgs(gpsLocation));
            }
        }

        public void OnProviderDisabled(string provider){ }

        public void OnProviderEnabled(string provider){}

        public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras){}
        #endregion
    }
但是,当设备未移动时,不会调用下面的
OnLocationChanged()
方法

 public class GpsLocationProvider : Java.Lang.Object, IGpsLocationProvider, ILocationListener
    {
        public event EventHandler<GpsLocationReceivedEventArgs> GpsLocationReceived;

        private LocationManager _locationManager;

        //call this method from other method to register during init.
        public void StartLocationUpdates(long minLocationUpdateIntervalInMs)
        {          
            if (_locationManager == null)
            {
                _locationManager = (LocationManager)Application.Context.GetSystemService(Context.LocationService);
            }

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

        public void StopLocationUpdates()
        {

            _locationManager?.RemoveUpdates(this);
        }

        #region ILocationListener Implementation

        public void OnLocationChanged(Location location)
        {
            if (GpsLocationReceived != null)
            {
                var gpsLocation = CreateLocation(location);

                GpsLocationReceived(this, new GpsLocationReceivedEventArgs(gpsLocation));
            }
        }

        public void OnProviderDisabled(string provider){ }

        public void OnProviderEnabled(string provider){}

        public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras){}
        #endregion
    }
公共类GpsLocationProvider:Java.Lang.Object、IGpsLocationProvider、ILocationListener
{
公共事件事件处理程序GpsLocationReceived;
私人场所经理(场所经理);;
//从其他方法调用此方法以在初始化期间注册。
公共位置更新(长minLocationUpdateIntervalInMs)
{          
if(_locationManager==null)
{
_locationManager=(locationManager)Application.Context.GetSystemService(Context.LocationService);
}
_locationManager.RequestLocationUpdate(locationManager.GpsProvider,1000,0,本);
}
公共void StopLocationUpdates()
{
_locationManager?.RemoveUpdate(此);
}
#区域ILocationListener实现
已更改位置上的公共无效(位置)
{
如果(GpsLocationReceived!=null)
{
var gpsLocation=CreateLocation(位置);
GpsLocationReceived(这是新的GpsLocationReceivedEventArgs(gpsLocation));
}
}
公共无效OnProviderDisabled(字符串提供程序){}
公共无效OnProviderEnabled(字符串提供程序){}
public void OnStatusChanged(字符串提供程序,[GeneratedEnum]可用性状态,Bundle extra){}
#端区
}
Xamarin Android版本8

棒棒糖21

更新:


谷歌播放服务不可用。因此FusedLocationProvider不可用。

我建议您尝试一下
FusedLocationProviderClient
,我仍然不明白谷歌为什么还没有弃用
ILocationListener
。顾名思义,它是一家“融合式”定位提供商,它将GPS和网络提供的定位相结合,让您在精确度和电池消耗之间进行权衡。它也可以独立于操作系统进行更新,因此更新的发布周期更短

基本上,您需要位置回调的一个子类:

    public class FusedLocationProviderCallback : LocationCallback
{
    readonly MainActivity activity;

    public FusedLocationProviderCallback(MainActivity activity)
    {
        this.activity = activity;
    }

    public override void OnLocationAvailability(LocationAvailability locationAvailability)
    {
        Log.Debug("FusedLocationProviderSample", "IsLocationAvailable: {0}",locationAvailability.IsLocationAvailable);
    }


    public override void OnLocationResult(LocationResult result)
    {
        if (result.Locations.Any())
        {
            var location = result.Locations.FirstOrDefault();
          //Do something
        }
        else
        {
           //Do something else if no location received.
        }
    }
}
然后让您的位置客户端设置类似这样的设置,我目前正在使用它来解决类似的问题。 此插件的一部分允许您将其设置为触发时间跨度或行驶距离。因此,即使用户在时间跨度内没有移动,它仍将获得一个位置。因此,即使用户没有移动,它也应该能解决你每秒都想要定位的问题

注1: 如果你想让它在Android的后台运行,你必须把它放在前台服务中

注2: 作为补充说明。我的用例每10秒或每10米询问一次位置。超过10分钟的时间,这些地点相当多。因此,我不会每次更新都发送位置,而是将其保存在本地SQLlite中,当保存了一定数量的位置或用户停止跟踪时,记录会被发送到服务器。假设你想每秒都得到这个位置,你会有更多的位置,所以这可能是一个需要考虑的问题。
如果您还有其他问题,请随时提问。

Google Play服务不可用。因此FusedLocationProvider不可用。请尝试此已使用的方法。你有游戏服务基地吗?在您的Android项目中引用?不,仅使用ILocationListener。我想了解为什么它不起作用,因为语句“\u locationManager.RequestLocationUpdates(locationManager.GpsProvider,1000,0,this);”指定每秒捕获位置,即使距离为零。从android文档中我可以看到,LocationListener的onLocationChanged仅在位置与上次实际不同时才会被调用,如果您随意使用方法名称,这是有意义的。我上面链接的插件总是在满足时间或距离的情况下返回一个位置,而不仅仅是在满足时间或距离并且位置发生了变化的情况下。