使用 DDMS、telnet 或任何其他方式将 GPS 坐标发送到 Android 模拟器
DDMS 无法将位置发送到模拟器。我已尝试仅从 DDMS 发送位置,但模拟器仍然无法接收位置。当我单击“发送”按钮时,DDMS 日志上没有显示任何内容。
我尝试从 telnet 发送地理修复,它返回 OK,但实际上并没有更新位置,或者如果更新了,我无法通过我的应用程序读取它。
应用程序在设备中正常工作,能够捕获测试位置详细信息,但无法捕获通过 DDMS 或 telnet 发送到模拟器的位置数据。
我正在 Android 2.2 模拟器上进行测试。谁能让我知道出了什么问题吗?
我的应用程序(如下)是使用 Android Mono 使用 C# 编写的,可能需要修复(我是 Android 的新手,所以我可能会错过一些东西)。 OnLocationChanged(Location location) 似乎根本没有触发,就好像监听器没有正确定义一样。任何帮助表示赞赏。
注意:我第一次运行此活动时,LocationManager.GetLastKnownLocation 为空,但未访问测试提供程序内容。当我再次运行它时,GetLastKnowLocation 仍然为空,但测试提供程序的内容已被访问和设置。诡异的。
[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 让我走到这一步。
DDMS is not able to send location to the emulator. I have tried sending just the location from DDMS but still the emulator is not able to receive location. Nothing appears on the DDMS log when I click the Send button.
I tried sending geo fix from telnet which returns OK but doesn't actually update the location, or if it does I can't read it via my application.
The application works properly in the device, is able to capture test location details but not able to capture location data sent to the emulator either via DDMS or telnet.
I am testing on Android 2.2 emulator. Can anyone let me know what is wrong?
My app (below) is written in C# using Mono for Android and may need fixing (I'm a newbie to all things Android so I could have missed something). OnLocationChanged(Location location) just doesn't seem to fire at all, as if the listener isn't properly defined. Any help appreciated.
Note: The first time I run this Activity the LocationManager.GetLastKnownLocation is null but the test provider stuff isn't accessed. When I run it again GetLastKnowLocation is still null but the test provider stuff is accessed and set. Weird.
[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){}
}
Kudos to https://stackoverflow.com/users/170333/greg-shackles for getting me this far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为问题可能在于您如何调用
RequestLocationUpdates()
。第三个参数是设备在获得更新之前需要移动的最小距离,因此您告诉系统仅在设备移动 2 米后才发送更新。如果它在真实设备上运行,可能是因为您移动了超过 6 英尺。 :)尝试从
RequestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this)
开始。这将在真实设备上启动一系列更新,但只有当您在 DDMS 中按“发送”时才会启动更新流。一旦成功,我会从那里开始计算您获取更新的频率。此外,当您启动模拟器时,
GetLastKnownLocation()
始终为 null。它对设备来说更好,因为它可以向您发送网络位置作为起始估计,或者如果最近有其他程序正在使用它,则可以向您发送 GPS 位置。编辑
这也可能是权限问题。通常您需要更改
AndroidManifest.xml
才能获取 GPS 访问权限。该行是请参阅此处的文档。
I think the problem may be with how you're calling
RequestLocationUpdates()
. That third parameter is the minimum distance the device needs to move before you get updates, so you're telling the system to only send updates after the device has moved 2 meters. If it works on a real device, it's probably because you moved more than 6 feet. :)Try starting with
RequestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this)
. That will start a stream of updates on a real device, but only one when you press 'Send' in DDMS. Once that works, I would work back from there on how often you get updates.Also,
GetLastKnownLocation()
is always null when you start the emulator. It's better for devices since it can send you the network location as a starting estimate, or the GPS location if another program was using it recently.EDIT
It could also be a permissions issue. Normally you need to alter
AndroidManifest.xml
to get GPS access. The line isSee the docs here.
在执行任何位置操作之前调用
函数,如下所示:
注意:程序在第一次运行后正常工作,因此在第一次运行后,您的应用程序可以从 telnet 获取初始更新的位置,这足以不会引发异常
Call
function before doing any location operations, like below:
Note: Its normal that program works after the first run, so that after the first run your application could get the initial updated location from telnet and that will be enough to not throws an exception
终于解决了这个问题。当VS2010启动模拟器时(即F5,开始调试),它的行为不符合预期。使用 AVD.exe 从外部启动模拟器,启动虚拟设备并部署应用程序。到它(使用F5,开始调试),一切正常。
为什么从 VS2010 内部或外部启动模拟器会产生任何影响,这是我能够接受的一个谜。感谢大家的有益建议。
Finally resolved this. When the emulator is launched by VS2010 (i.e. F5, start debugging) it does not behave as expected. Launch the emulator externally using AVD.exe, start a virtual device and deploy the app. to it (using F5, start debugging) and everything works fine.
Why starting the emulator from within or outside VS2010 should make any difference is a mystery I am able to live with. Thanks to everyone for their helpful suggestions.
你的模拟 Android 镜像有 GPS 硬件吗?模拟器中的描述应该有“hw.gps=yes”。
在使用正确的(模拟)硬件重新创建新映像之前,我遇到了相同的症状。我发现一个显示当前位置的简单网页在调试仿真环境时非常方便。
Does your emulated android image have GPS hardware? The description in the emulator should have "hw.gps=yes".
I had the same symptoms before recreating a new image with the right (emulated) hardware. I found a simple web page that displays the current location was handy when debugging the emulation environment.