我对Android开发相当陌生,而且我正在尝试开发一个应用程序以适应新的应用程序的运行。有时候这个应用程序似乎工作正常,但大部分时间没有。我试图从应用程序简单地设置设备的GPS纬度/长度;没有什么比这真的。我利用了从其他帖子收集的大量知识,但最终,我不确定我的应用程序是否因模拟器怪癖而无法工作,或者它是否是我的代码。作为参考,我使用IntelliJ,我的目标是2.3.3,我确实在我的模拟器上启用了GPS,并且确实在我的清单中设置了适当的权限。Android - 在IntelliJ模拟器问题中模拟位置
奇怪的是,当我打开模拟器时,它有时可以正常工作,然后我通过终端telnet并通过“geo fix”设置地理位置,然后返回到应用程序,并显示我的GPS lat/long我输入的。但通常它根本不起作用。当我试图通过代码中的模拟位置以编程方式执行时,我会得到相同的结果。
这里是我在我的onCreate()方法的顶部代码(的LocationManager是一个类级字段):
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, true, true, 0, 5);
..和当用户键入一个纬度/经度进UI和轻敲提交按钮,被提取的字段经过下面的代码被称为和一些轻验证完成:
Location mLocation = new Location(LocationManager.GPS_PROVIDER);
mLocation.setLatitude(Float.valueOf(inputLat));
mLocation.setLongitude(Float.valueOf(inputLong));
mLocation.setTime(System.currentTimeMillis());
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mLocation);
refreshTheLocation();
而对于参考,refreshTheLocation()方法是:
public void refreshTheLocation() {
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
final Location finalLoc = location;
if (location != null) {
runOnUiThread(new Runnable() {
public void run() {
TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
gpsTextView.setText("GPS DATA FOUND:\nLatitude: " + finalLoc.getLatitude() + "\nLongitude: " + finalLoc.getLongitude());
}
});
} else {
runOnUiThread(new Runnable() {
public void run() {
TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
gpsTextView.setText("GPS data not found");
}
});
}
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
的“MyLocation”类只是对这个职位的副本/从收视率最高的答案贴:
What is the simplest and most robust way to get the user's current location on Android?
对我的问题可能是什么有什么想法?它似乎大约有1/5的时间,它获得GPS坐标并显示它们,但其他4/5的时间,它不是。当它工作时,当我通过输入栏和按钮改变坐标时,它不会更新坐标。
对我来说很容易,因为我对这个东西相当陌生。 ;)提前感谢您提供的任何帮助!
工作就像一个魅力!就是这样,谢谢你!作为参考,我发现一篇文章说2.3.3有一个GPS的错误,所以我实际上使用2.2(我原来的帖子是错的)。但无论如何,我需要使用2.3.3,所以我很高兴你让我知道这一点。出于好奇,Android和这些API的Google版本有什么区别?我应该一直使用Google的吗?或者什么是标准? – svguerin3 2012-07-26 14:20:02
如果您在应用程序中进行Google地图集成(MapActivity等),则需要使用该版本的AVD。 – Romin 2012-07-26 14:57:24
感谢Romin - 这绝对有道理,但实际上我没有在我的应用中使用任何类型的Google地图集成。我发布的代码实际上是应用中唯一的代码。我猜测GPS的东西(或者也许是模拟位置)也需要它? – svguerin3 2012-07-26 15:10:29