Android 在 xamarin 中构建后台位置需要永远

Android background location build in xamarin is taking forever

所以我正在使用一个应用程序,它使用 GPS 数据获取当前纬度、经度、当前速度等,该应用程序没有给我任何错误并成功部署,我正在使用我的 phone (Sony Xperia D 2005) 来部署它。 无论如何,它说 "Waiting on location updates" 但它要花很长时间...... 对不起,我要粘贴的所有代码行,但我不知道问题出在哪里。 这是我的 MainActivity.cs 代码:(我是 xamarin 的新手,我不知道这部分代码是否是造成问题的原因,所以如果您需要任何其他代码,请告诉我...)

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Util;
using Android.Locations;
using Location.Droid.Services;
using Android.Content.PM;

namespace Location.Droid
{
[Activity (Label = "LocationDroid", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | 
ConfigChanges.Orientation | ConfigChanges.ScreenLayout)]
public class MainActivity : Activity
{
    readonly string logTag = "MainActivity";

    // make our labels
    TextView latText;
    TextView longText;
    TextView altText;
    TextView speedText;
    TextView bearText;
    TextView accText;

    #region Lifecycle

    //Lifecycle stages
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        Log.Debug (logTag, "OnCreate: Location app is becoming active");

        SetContentView (Resource.Layout.Main);

        // This event fires when the ServiceConnection lets the client (our App class) know that
        // the Service is connected. We use this event to start updating the UI with location
        // updates from the Service
        App.Current.LocationServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
            Log.Debug (logTag, "ServiceConnected Event Raised");
            // notifies us of location changes from the system
            App.Current.LocationService.LocationChanged += HandleLocationChanged;
            //notifies us of user changes to the location provider (ie the user disables or enables GPS)
            App.Current.LocationService.ProviderDisabled += HandleProviderDisabled;
            App.Current.LocationService.ProviderEnabled += HandleProviderEnabled;
            // notifies us of the changing status of a provider (ie GPS no longer available)
            App.Current.LocationService.StatusChanged += HandleStatusChanged;
        };

        latText = FindViewById<TextView> (Resource.Id.lat);
        longText = FindViewById<TextView> (Resource.Id.longx);
        altText = FindViewById<TextView> (Resource.Id.alt);
        speedText = FindViewById<TextView> (Resource.Id.speed);
        bearText = FindViewById<TextView> (Resource.Id.bear);
        accText = FindViewById<TextView> (Resource.Id.acc);

        altText.Text = "altitude";
        speedText.Text = "speed";
        bearText.Text = "bearing";
        accText.Text = "accuracy";

        // Start the location service:
        App.StartLocationService();
    }

    protected override void OnPause()
    {
        Log.Debug (logTag, "OnPause: Location app is moving to background");
        base.OnPause();
    }


    protected override void OnResume()
    {
        Log.Debug (logTag, "OnResume: Location app is moving into foreground");
        base.OnResume();
    }

    protected override void OnDestroy ()
    {
        Log.Debug (logTag, "OnDestroy: Location app is becoming inactive");
        base.OnDestroy ();

        // Stop the location service:
        App.StopLocationService();
    }

    #endregion

    #region Android Location Service methods

    ///<summary>
    /// Updates UI with location data
    /// </summary>
    public void HandleLocationChanged(object sender, LocationChangedEventArgs e)
    {
        Android.Locations.Location location = e.Location;
        Log.Debug (logTag, "Foreground updating");

        // these events are on a background thread, need to update on the UI thread
        RunOnUiThread (() => {
            latText.Text = String.Format ("Latitude: {0}", location.Latitude);
            longText.Text = String.Format ("Longitude: {0}", location.Longitude);
            altText.Text = String.Format ("Altitude: {0}", location.Altitude);
            speedText.Text = String.Format ("Speed: {0}", location.Speed);
            accText.Text = String.Format ("Accuracy: {0}", location.Accuracy);
            bearText.Text = String.Format ("Bearing: {0}", location.Bearing);
        });

    }

    public void HandleProviderDisabled(object sender, ProviderDisabledEventArgs e)
    {
        Log.Debug (logTag, "Location provider disabled event raised");
    }

    public void HandleProviderEnabled(object sender, ProviderEnabledEventArgs e)
    {
        Log.Debug (logTag, "Location provider enabled event raised");
    }

    public void HandleStatusChanged(object sender, StatusChangedEventArgs e)
    {
        Log.Debug (logTag, "Location status changed, event raised");
    }

    #endregion

}

}

在这种情况下,您应该同时注册 GPS 和 NETWORK 提供商。

LocMgr.RequestLocationUpdates(LocationManager.GpsProvider, 2000, 0, this);
LocMgr.RequestLocationUpdates(LocationManager.NetworkProvider, 2000, 0, this);

GPS 提供商在室外工作,NETWORK 使用基站和 wifi。

这篇article可以帮助您优化代码。