我在Android上的gps实现有问题。问题是GPS丢失修复。位置图标是脉动的,经过几秒钟的吐司显示,然后GPS失去修复并再次尝试连接。
我的服务:
public class LocationService extends Service {
private static final long MIN_TIME = 15000;
private static final float MIN_DISTANCE = 1;
private final IBinder binder = new LocalBinder();
private LocationManager locationManager;
@Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class LocalBinder extends Binder {
LocationService getService() {
return LocationService.this;
}
}
public void setListener(LocationListener listener) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, listener);
}
public void removeListener(LocationListener listener) {
locationManager.removeUpdates(listener);
}
}
我的活动:
public class MainMapActivity extends CustomMapActivity implements LocationListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_layout);
Intent i = new Intent(this, LocationService.class);
startService(i);
bindService(i, conn, BIND_AUTO_CREATE);
}
@Override
public void onDestroy() {
super.onDestroy();
unbindService(conn);
}
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
serv = ((LocalBinder)service).getService();
serv.setListener(MainMapActivity.this);
}
};
@Override
public void onLocationChanged(Location location) {
Toast.makeText(this, Double.toString(location.getLatitude())+Double.toString(location.getLongitude()), Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
这不是设备的问题,因为在谷歌地图应用程序中它是完美的。
感谢您的帮助
答案 0 :(得分:1)
您已将最小时间参数15秒传递给位置监听器。在大多数设备/操作系统版本中,这将在重新启用之前关闭GPS一段时间。