永远不会调用Android OnLocationChanged

时间:2013-03-12 04:47:46

标签: android

我有以下代码 - enter code here
    使用系统;     使用System.Collections.Generic;     使用System.Text;     使用Android.App;     使用Android.Content;     使用Android.Runtime;     使用Android.Views;     使用Android.Widget;     使用Android.OS;     使用Android.Locations;     使用Android.Bluetooth;

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上运行此代码。

1 个答案:

答案 0 :(得分:0)

如果您在模拟器上运行,则除非您从DDMS提供位置,否则onLocationChanged将永远不会被调用。从DDMS您需要给出纬度和经度,以便它可以调用所需的方法。

请检查开放区域中真实设备上的代码。