在服务中未调用位置更新

时间:2013-04-24 17:31:01

标签: android service background gps

我正在开发一个需要在一段时间内检索用户位置的应用程序。我在活动中使用了LocationListener接口并且它工作正常,但是当我在我的android服务中实现它时,它没有被调用。这是onCreate方法:

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); ...}

我真的很感激任何帮助,服务似乎很好,因为我发送Toast并且它们被发送,但onLocationUpdate从未被调用。

1 个答案:

答案 0 :(得分:0)

编辑:正如Shobhit Puri所说,它只需要在onStartCommand()

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;
}