xamarin 中的 Android 后台位置构建需要永远
Posted
技术标签:
【中文标题】xamarin 中的 Android 后台位置构建需要永远【英文标题】:Android background location build in xamarin is taking forever 【发布时间】:2017-09-20 17:57:47 【问题描述】:所以我正在使用一个应用程序,它可以获取我当前的纬度、经度、当前速度等,使用 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
【问题讨论】:
确保您获得适当的权限,请查看github.com/xamarin/mobile-samples/blob/master/… 【参考方案1】:在这种情况下,您应该同时注册 GPS 和 NETWORK 提供商。
LocMgr.RequestLocationUpdates(LocationManager.GpsProvider, 2000, 0, this);
LocMgr.RequestLocationUpdates(LocationManager.NetworkProvider, 2000, 0, this);
GPS 提供商在外面工作,而 NETWORK 使用手机信号塔和 wifi。
这个article 可以帮助您如何优化您的代码。
【讨论】:
以上是关于xamarin 中的 Android 后台位置构建需要永远的主要内容,如果未能解决你的问题,请参考以下文章
Google Play 服务 - 位置 Xamarin Android