DDMS 无法将位置发送到模拟器。我已尝试仅从 DDMS 发送位置,但模拟器仍然无法接收位置。当我单击“发送”按钮时,DDMS 日志上没有显示任何内容。
我尝试从 telnet 发送地理修复,它返回 OK,但实际上并没有更新位置,或者如果更新了,我无法通过我的应用程序读取它。
应用程序在设备中正常工作,能够捕获测试位置详细信息,但无法捕获通过 DDMS 或 telnet 发送到模拟器的位置数据。
我正在 Android 2.2 模拟器上进行测试。谁能告诉我出了什么问题吗?
我的应用程序(如下)是使用 Android Mono 用 C# 编写的,可能需要修复(我是 Android 的新手,所以我可能会错过一些东西)。 OnLocationChanged(Location location) 似乎根本没有触发,就好像监听器没有正确定义一样。任何帮助表示赞赏。
注意:我第一次运行此 Activity 时,LocationManager.GetLastKnownLocation 为 null,但未访问测试提供程序内容。当我再次运行它时,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让我走到这一步。
最佳答案
我认为问题可能在于您如何调用 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 .
关于android - 使用 DDMS、telnet 或任何其他方式将 GPS 坐标发送到 Android 模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897387/