我有以下代码 -
enter code here
使用系统;使用 System.Collections.Generic;使用 System.Text;使用Android.App;使用Android.Content;使用 Android.Runtime;使用 Android.Views;使用 Android.Widget;使用Android.OS;使用 Android.Locations;使用安卓.蓝牙;
namespace GetCurrentLocation
{
[Activity(Label = "Get Current Location", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity, ILocationListener
{
LocationManager _locMgr ;
String oBestProvider;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_locMgr = GetSystemService(Context.LocationService) as LocationManager;
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.UI);
StringBuilder strLocations = new StringBuilder();
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.btnGetLocation);
int i;
var locationText =
FindViewById<TextView>(Resource.Id.txtLocation);
Android.Locations.Location oLocation;
oLocation = _locMgr.GetLastKnownLocation (Context.LocationService);
IList<String> oLocationProviders = _locMgr.GetProviders(true);
//Location location;
button.Click += delegate {
if(oLocationProviders != null)
{
_locMgr.RequestLocationUpdates(oLocationProviders[0], 2000, 1, this);
_locMgr.RequestLocationUpdates(oLocationProviders[1], 2000, 1, this);
}
else{
locationText.Text ="No Provider Found";
}
};
}
protected override void OnResume() {
base.OnResume();
}
protected override void OnPause() {
base.OnPause ();
_locMgr.RemoveUpdates(this);
}
public void OnLocationChanged (Location location)
{
var locationText =
FindViewById<TextView> (Resource.Id.txtLocation);
if (location != null) {
locationText.Text = String.Format ("Latitude = {0}, Longitude = {1}",
location.Latitude, location.Longitude);
}
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
}
}
}
我尝试过使用 GetBestProvider/GetProviders/RequestLocationUpdates/GetLastKnownLocation。我无法继续。有人可以指导我为什么我的 OnLocationUpdates 永远不会被调用。即使类 Activity 正在实现 ILocationListener 接口,我也无法在此函数上使用 Override 关键字。请帮助.....也只是为了您的信息,我正在尝试在 Google 的试用版 Emulator AVD For Nexus 5 上运行此代码。