无法获得MonoDroid的位置更新 [英] Unable to get the location updates in monodroid

查看:131
本文介绍了无法获得MonoDroid的位置更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid应用程序,以获得位置如下:

I am trying to get the location in my Android app as follows

LocationManager lmanager = (LocationManager)context.GetSystemService(Context.LocationService);
if (lmanager.IsProviderEnabled(LocationManager.NetworkProvider))
 {
  Location currentLoc = lmanager.GetLastKnownLocation(LocationManager.NetworkProvider);
}

我试着用 LocationManager.gpsProvider 也而非网络供应商。

在所有的情况下,我得到一个空引用异常为 currentLoc

In all the cases I am getting a null reference exception for currentLoc.

推荐答案

的<一个href=\"http://docs.mono-android.net/?link=M%3aAndroid.Locations.LocationManager.GetLastKnownLocation%28System.String%29\"相对=nofollow> GetLastKnownLocation() 方法返回它知道最后的位置,如果它从未获得过任何更新可能为空。我想你想在这里做什么是监听位置更新,然后回应他们,一旦他们进来,你可以通过实现的 ILocationListener 接口:

class MyLocationListener : Java.Lang.Object, ILocationListener
{
    public void OnLocationChanged(Location location)
    {
    }

    public void OnProviderDisabled(string provider)
    {
    }

    public void OnProviderEnabled(string provider)
    {
    }

    public void OnStatusChanged(string provider, Availability status, Bundle extras)
    {
    }
}

在你的活动你可以定义你想使用这个类来开始监听像这样的更新:

In your activity you can declare that you'd like to use this class to start listening for updates like this:

var locationManager = (LocationManager)GetSystemService(LocationService);
var locationListener = new MyLocationListener();

locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 5000, 2, locationListener);

这篇关于无法获得MonoDroid的位置更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆