Android onLocationUpdate在服务中未被调用
我正在开发一个需要在一段时间内检索用户位置的应用程序。我在活动中使用过LocationListener接口,它工作得很好,但是当我在android服务中实现它时,它没有被调用。以下是onCreate方法:Android onLocationUpdate在服务中未被调用,android,service,background,gps,Android,Service,Background,Gps,我正在开发一个需要在一段时间内检索用户位置的应用程序。我在活动中使用过LocationListener接口,它工作得很好,但是当我在android服务中实现它时,它没有被调用。以下是onCreate方法: public void onCreate() { super.onCreate(); turned= true; locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
public void onCreate() {
super.onCreate();
turned= true;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
locationManager
.requestLocationUpdates(provider, 0, 0, this); ...}
我真的很感激任何帮助,服务似乎很好,因为我发送了土司,它们也被发送了,但从未调用onLocationUpdate 编辑:正如Shobhit Puri所说,它只需要在onStartCommand()中
你确定它真的有GPS锁吗?这可能需要一段时间。引用:我在onStartCommand()内部调用它,而不是OnCreate(),它适用于我。您可能想试试。非常感谢Shobhit Puri,您的回答解决了我的问题。位置侦听器在服务中工作正常。1.检查是否有GPS定位。2.如何实现LocationListener?
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onCreate();
encendido = true;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
locationManager
.requestLocationUpdates(provider, 0, 0, this);
return START_STICKY;
}