Сборка фонового местоположения Android в xamarin занимает вечность

Итак, я работаю с приложением, которое получает текущую широту, долготу, текущую скорость и т. д., используя данные GPS. Приложение не выдает никаких ошибок и успешно развертывается, и я использую свой телефон (Sony Xperia D 2005) для развертывания. Это. Во всяком случае, он говорит «Ожидание обновлений местоположения», но это занимает вечность ... Извините за все строки кода, которые я собираюсь вставить, но я не знаю, в чем проблема. Вот мой код 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

}

}


person Fatjon Rrapaj    schedule 24.04.2017    source источник
comment
убедитесь, что у вас есть правильные разрешения, пожалуйста, проверьте github.com/xamarin/mobile-samples/blob/master/   -  person Mina Fawzy    schedule 26.04.2017


Ответы (1)


В этом случае вам необходимо зарегистрировать обоих провайдеров GPS и NETWORK.

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

Поставщик GPS работает снаружи, а СЕТЬ использует вышки сотовой связи и Wi-Fi.

Эта статья поможет вам оптимизировать код.

person Jan Nepraš    schedule 19.07.2017