c# - xamarin 中构建的 Android 后台位置正在永远占用

标签 c# xamarin xamarin.android location android-gps

所以我正在使用一个应用程序,它可以获取当前的纬度、经度、当前速度等,使用 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

}

最佳答案

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

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

GPS 提供商在室外工作,NETWORK 使用手机信号塔和 wifi。

article可以帮助你如何优化你的代码。

关于c# - xamarin 中构建的 Android 后台位置正在永远占用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581541/

相关文章:

c# - Base Controller ASP.NET MVC 3 中的这个 Custom Principal 是不是非常低效?

c# - 是否可以在 PropertyGrid 中查看公共(public)属性以外的成员?

java - OnPostExecute 永远不会被调用

c# - 使用 C# 的 Android 和 IOS 自定义 UI 控件

c# - 有没有一种稳定的方法可以在 Android 设备上获取电话号码?

C# 从多个抽象类派生类

c# - 在 ASP.Net C# Webform 的 HTML 表格中显示 DataTable

ios - 单击 UICollectionView 中的自定义单元格时如何触发 segue

xaml - Xamarin 标签单击更改复选框状态仅 Xaml

xamarin - Android 9.0 作为 Visual Studio 2019 中的目标 Android 版本?