【发布时间】:2011-11-08 08:07:34
【问题描述】:
查找设备当前位置的测试应用在模拟器中运行良好。也就是说,当我在 Eclipse 中的 DDMS 中,我将位置坐标发送到模拟器,应用程序捕获广播并在模拟器的地图中显示位置。但在我真正的 2.3.3 android 设备中,应用程序只是挂起,而 gps 在通知栏中闪烁。这是我在服务类中所做的事情
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("<<MyGpsService-onStart>>", "GPS starting...");
triggerService = new Thread(new Runnable() {
public void run() {
try {
Looper.prepare();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
myLocationListener = new GPSListener();
long minTime = 10000; // frequency update: 10 seconds
float minDistance = 50; // frequency update: 50 meter
lm.requestLocationUpdates(
// request GPS updates
LocationManager.GPS_PROVIDER, minTime, minDistance,
myLocationListener);
Looper.loop();
} catch (Exception e) {
Log.e("MYGPS", e.getMessage());
}
}// run
});
triggerService.start();
}
class GPSListener implements LocationListener {
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Intent myFilteredResponse = new Intent(GPS_FILTER);
myFilteredResponse.putExtra("latitude", latitude);
myFilteredResponse.putExtra("longitude", longitude);
Log.e(">>GPS_Service<<", "Lat:" + latitude + " lon:" + longitude);
sendBroadcast(myFilteredResponse);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};// GPSListenerclass
我想知道你们中是否有人有类似的问题。谢谢。
更新:我遵循以下问题和答案:
Android - Reliably getting the current location
更改后,应用运行得更好了。
【问题讨论】:
-
当 GPS 闪烁时,表示您没有 GPS 锁定(通常是由于在室内信号不佳)。你试过站在外面等着拿到锁吗?执行此操作后,GPS 图标将停止闪烁并保持稳定。
-
感谢您的评论。现在我得到了不正确位置的坐标。地图上的位置距离这里 5 英里。也许这又是一个信号问题。但是当我尝试谷歌地图时,它显示了正确的位置。
-
您可能应该创建一些阈值并继续收听,直到您的位置落入其中(即 location.getAccuracy())。然后停止监听位置。
-
哦,原来是我的计算错误。我不小心把坐标整数弄错了。
标签: android geolocation