服务不提供位置更新

Service is not giving Location Updates

我正在尝试让某个应用在后台运行时提供位置更新。当应用程序打开时,服务工作正常。当应用程序在后台时,服务继续 运行 但位置更新停止。我试过使用前台服务,但这没有帮助。

我正在使用 google 的 fusedLocationProviderClient

public int onStartCommand(Intent intent, int flags, int startId) {
      client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}

使用 onCreate() 中定义的位置请求

locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(500);
locationRequest.setFastestInterval(500);

回调定义为:

private final LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        List<Location> locationList = locationResult.getLocations();
        if (locationList.size() != 0) {
            Location location = locationList.get(0);
            Log.e("AppLocationService", "Latitude  - " +location.getLatitude()+", longitude  - " +location.getLongitude() );
        }
    }

我是 android 工作室的新手,非常感谢任何帮助!

更新:

服务通过 startService() 启动

public class BackgroundService2 extends Service {
private FusedLocationProviderClient client;
private LocationRequest locationRequest;

@Override
public void onCreate() {
    super.onCreate();
    client = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(500);
    locationRequest.setFastestInterval(500);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent,flags,startId);
    try {
        if ( Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission( getBaseContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            client.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
        }

    } catch (SecurityException ignore) {
        Log.e("AppLocationService", "SecurityException - " + ignore.toString(), ignore);
    }
    return START_STICKY;
}


private final LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        List<Location> locationList = locationResult.getLocations();
        if (locationList.size() != 0) {
            Location location = locationList.get(0);
            Log.e("AppLocationService", "Latitude  - " +location.getLatitude()+", longitude  - " +location.getLongitude() );
        }
    }

};


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

}

原来是我的清单有问题。不仅要声明服务,还要声明其类型。

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

以及

<service
        android:name=".BackgroundService"
        android:enabled="true"
        android:exported="true"
        android:foregroundServiceType="location" />