0

我正在开发一个需要在一段时间内检索用户位置的应用程序。我在活动中使用了 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。

4

1 回答 1

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;
}
于 2015-09-22T17:48:00.933 回答