使用DDMS,telnet或任何其他方式将GPS坐标发送到Android模拟器

时间:2012-01-17 15:37:38

标签: android gps xamarin.android telnet ddms

DDMS无法将位置发送到模拟器。我试过从DDMS发送只是位置,但模拟器仍然无法接收位置。单击“发送”按钮时,DDMS日志上没有任何内容。

我尝试从telnet发送geo fix,返回OK但实际上并没有更新位置,如果是,我无法通过我的应用程序读取它。

应用程序在设备中正常运行,能够捕获测试位置详细信息,但无法通过DDMS或telnet捕获发送到模拟器的位置数据。

我在Android 2.2模拟器上测试。谁能让我知道出了什么问题?

我的应用程序(如下)是使用Mono for Android用C#编写的,可能需要修复(我是Android的所有东西的新手,所以我可能错过了一些东西)。 OnLocationChanged(位置位置)似乎根本没有触发,就像没有正确定义侦听器一样。任何帮助赞赏。

注意:第一次运行此Activity时,LocationManager.GetLastKnownLocation为null,但不访问测试提供程序的内容。当我再次运行它时,GetLastKnowLocation仍为null,但访问并设置了测试提供程序的内容。奇怪的。

[Activity(Label = "Location Demo")]
public class LocationActivity : Activity, ILocationListener
{
    private TextView _locationText;
    private LocationManager _locationManager;
    private StringBuilder _builder;
    private Geocoder _geocoder;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.LocationActivity);
        _geocoder = new Geocoder(this);
        _locationText = FindViewById<TextView>(Resource.Id.TextView1);
        _locationManager = (LocationManager)GetSystemService(LocationService);

        if (_locationManager.GetLastKnownLocation("gps") == null)
        {
            _locationManager.AddTestProvider("gps", false, false, false, false, false, false, false, 0, 5);
            _locationManager.SetTestProviderEnabled("gps", true);
            Location loc = new Location("gps");
            loc.Latitude = 50;
            loc.Longitude = 50;
            _locationManager.SetTestProviderLocation("gps", loc);
        }
        Location lastKnownLocation = _locationManager.GetLastKnownLocation("gps");

        if (lastKnownLocation != null)
        {
            _locationText.Text += string.Format("Last known location, lat: {0}, long: {1}", lastKnownLocation.Latitude, lastKnownLocation.Longitude);
        }
        else
        {
            _locationText.Text += string.Format("Last location unknown");
        }
        _locationManager.RequestLocationUpdates("gps", 5000, 2, this);
    }
    public void OnLocationChanged(Location location)
    {
        _locationText.Text += string.Format("Location updated, lat: {0}, long: {1}", location.Latitude, location.Longitude);
    }
    public void OnProviderDisabled(string provider){}
    public void OnProviderEnabled(string provider){}
    public void OnStatusChanged(string provider, Android.Locations.Availability availability, Bundle extras){}

}

感谢https://stackoverflow.com/users/170333/greg-shackles让我这么远。

4 个答案:

答案 0 :(得分:2)

我认为问题可能在于您如何调用RequestLocationUpdates()。第三个参数是设备在获得更新之前需要移动的最小距离,因此您告诉系统仅在设备移动2米后才发送更新。如果它适用于真实设备,可能是因为你移动了6英尺以上。 :)

尝试从RequestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this)开始。这将在真实设备上启动更新流,但只有一个在DDMS中按“发送”时才会启动。一旦有效,我会从那里回来查看您获得更新的频率。

此外,启动模拟器时,GetLastKnownLocation()始终为null。这对设备来说更好,因为它可以将网络位置作为起始估计值发送给您,或者如果其他程序最近使用它,则会将GPS位置发送给您。

修改

它也可能是权限问题。通常,您需要更改AndroidManifest.xml才能获得GPS访问权限。这条线是

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

请参阅文档here

答案 1 :(得分:1)

致电

_locationManager.RequestLocationUpdates("gps", 5000, 2, this);

在执行任何位置操作之前的功能,如下所示:

base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.LocationActivity);
_geocoder = new Geocoder(this);
_locationText = FindViewById<TextView>(Resource.Id.TextView1);
_locationManager = (LocationManager)GetSystemService(LocationService);
_locationManager.RequestLocationUpdates("gps", 5000, 2, this); //this will cause updated location to be retrieved from telnet

注意:正常程序在第一次运行后工作,因此在第一次运行后,您的应用程序可以从telnet获取初始更新的位置,这将足以不引发异常

答案 2 :(得分:1)

终于解决了这个问题。当模拟器由VS2010启动时(即F5,开始调试),它的行为不符合预期。使用AVD.exe在外部启动模拟器,启动虚拟设备并部署应用程序。它(使用F5,开始调试),一切正常。

为什么从VS2010内部或外部启动模拟器应该有所作为,这是我能够忍受的一个谜。感谢大家的有益建议。

答案 3 :(得分:0)

您的模拟Android图像是否具有GPS硬件?模拟器中的描述应为“hw.gps = yes”。

在使用正确(模拟)硬件重新创建新图像之前,我遇到了相同的症状。我发现了一个简单的网页,在调试仿真环境时显示current location很方便。