我有以下代碼 - enter code here
using System; using System.Collections.Generic; using System.Text;使用Android.App的 ;使用Android.Content的 ;使用Android.Runtime的 ;使用Android.Views的 ;使用Android.Widget的 ; 使用Android.OS;使用Android.Locations的 ;使用Android.Bluetooth的 ;Android OnLocationChanged永不被稱爲
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關鍵字。請幫助..... 也只是爲了您的信息,我試圖在谷歌的試用版仿真器AVD For Nexus 5上運行此代碼。
我認爲你的意思是Nexus 4/Nexus S. ** Nexus 5 **尚未發佈。 ;-) – 2013-03-12 04:55:30