1

我正在使用 MvvmCross 框架进行跨设备开发。当我在 Android 模拟器中测试我的应用程序时(我还不能在物理设备上测试它),在 LocationManager 上调用 RequestLocationUpdates 时出现 Java.Lang.IllegalArgumentException 异常。另外,我不知道它是否相关,但是当我将鼠标悬停在语句上时,我被告知 RequestLocationUpdates 是一个“未知方法”。无论是否发生异常,都会发生这种情况。

它似乎只在我第二次在我的应用程序中调用它时发生,但是在使用之间清除它的代码看起来应该可以工作

    protected override void PlatformSpecificStart(MvxGeoLocationOptions options)
    {
        if (_locationManager != null)
            throw new MvxException("You cannot start the MvxLocation service more than once");

        _locationManager = (LocationManager)Context.GetSystemService(Context.LocationService);
        var criteria = new Criteria() { Accuracy = options.EnableHighAccuracy ? Accuracy.Fine : Accuracy.Coarse };
        var bestProvider = _locationManager.GetBestProvider(criteria, true);

        _locationManager.RequestLocationUpdates(bestProvider, 5000, 2, this);
    }

    protected override void PlatformSpecificStop()
    {
        EnsureStopped();
    }

    private void EnsureStopped()
    {
        if (_locationManager != null)
        {
            _locationManager.RemoveUpdates(this);
            _locationManager = null;
        }
    }

这个类继承自Java.Lang.Object,我已经验证了PlatformSpecificStart 和Stop 并在适当的时间调用(即Stop 肯定在第二次Start 之前调用)。谁能告诉我出了什么问题?

4

1 回答 1

1

我在教程中添加了一个课程,显示IMvxGeoLocationWatcher正在使用的界面。

请参阅https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20Tutorial/Tutorial/Tutorial.Core/ViewModels/Lessons/LocationViewModel.cs中的示例,代码如下:

    private void DoStartStop()
    {
        if (!IsStarted)
        {
            _watcher.Start(new MvxGeoLocationOptions() { EnableHighAccuracy = true }, OnNewLocation, OnError);
        }
        else
        {
            _watcher.Stop();
        }

        IsStarted = !IsStarted;
    }

    private void OnError(MvxLocationError error)
    {
        // TODO - shuold handle the error better than this really!
        LastError = error.Code.ToString();
    }

    private void OnNewLocation(MvxGeoLocation location)
    {
        if (location != null && location.Coordinates != null)
        {
            Lat = location.Coordinates.Latitude;
            Lng = location.Coordinates.Longitude;
        }
    }

这似乎可以正常工作:

我还没有为 WinRT 编写代码...


可能导致您的错误的一件事是,如果您尝试 Dispose()IMvxGeoLocationWatcher实例 - 这样做可能会导致不可预测的结果。

在 WM6、iPhone、Bada、WP7 和 Android for RunSat/Navmi (http://www.navmi.com) 上处理 GPS 时,我经常遇到原生平台可能非常“不稳定”/“不可预测”的问题。该应用程序启动/停止多个位置侦听器 - 我认为这确实影响了我设计IMvxGeoLocationWatcher功能的方式。一般来说,如果您的应用程序使用,IMvxGeoLocationWatcher那么我认为最好将对该位置的访问包装在通过接口访问的某种单例中 - 这样做可以让您更轻松地控制位置功能,而不是拥有多个客户端单独尝试启动/停止位置管理器。


如果你发现它的功能IMvxGeoLocationWatcher不是你想要的,那么一定要创建你自己的接口和你自己的平台相关实现——它们可以很容易地注入到每个原生的 Setup 类中。

例如,您可能想尝试的一件事是使用可用于位置的 Mono 移动扩展的界面(以及用于联系人和越来越多的其他功能)

于 2012-04-25T21:24:45.703 回答