在 Oreo (8.0.0+) (API 26+) 中,如何在应用程序处于后台或终止时获取位置服务更新

In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill

In Android O (8.0.0+) (API 26+), 如何在应用程序处于后台或被杀死时获取位置服务更新。在 "Strava" Android 应用程序(在 Play Store 上可用)中,定位服务 运行 在应用程序处于后台或终止时正常。我需要在我的应用程序中构建相同的功能。 每当我使用

开始服务时
startService(new Intent(this, GpsServices.class));

当应用程序处于空闲模式(应用程序处于后台或被终止)时,10 秒后调用 onDestroy 方法。下面是我的代码。

public class GpsServices extends Service implements LocationListener, Listener {

    @Override
    public void onCreate() {
        mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        if (mLocationManager != null) {
            mLocationManager.addGpsStatusListener(this);
        }
        if (mLocationManager != null) {
            mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                500,
                0,
                this);
        }    
    }

    public void onLocationChanged(Location location) {
        System.out.println("location = [" + location + "]");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    /* Remove the locationlistener updates when Services is stopped */
    @Override
    public void onDestroy() {
        try {
            mLocationManager.removeUpdates(this);
            mLocationManager.removeGpsStatusListener(this);
            stopForeground(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如有任何帮助,我们将不胜感激。

您可以尝试以下两个选项之一或两者的组合,这已经解决了我遇到的问题。

选项 1

要在后台继续 运行 位置更新,您必须在 CODEPATH 中使用 LocationServices API with FusedLocationProviderClient as described here and here in docs or here

选项 2

如果您在 here 的某处正确阅读了 Android Oreo 8.0 文档,您就会找到这个解决方案。

Step 1: Make sure you start a service as a foreground service as given in below code

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {

            mainActivity.startService(new Intent(getContext(), GpsServices.class));
            mainActivity.startService(new Intent(getContext(), BluetoothService.class));
            mainActivity.startService(new Intent(getContext(), BackgroundApiService.class));
        }
        else {
            mainActivity.startForegroundService(new Intent(getContext(), GpsServices.class));
            mainActivity.startForegroundService(new Intent(getContext(), BluetoothService.class));
            mainActivity.startForegroundService(new Intent(getContext(), BackgroundApiService.class));
        }

Step 2: Use notification to show that your service is running. Add below line of code in onCreate method of service.

@Override
public void onCreate() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(NOTIFICATION_ID, notification);
    }
    ...
}

Step 3: Remove the notification when the service is stopped or destroyed.

@Override
public void onDestroy() {
    ...
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          stopForeground(true); //true will remove notification
    }
    ...
}

选项 2 的一个问题是它会一直显示 notification 直到你的 GpsService 在所有设备上都是 运行 运行 在 Android 奥利奥 8.0.

我确信即使应用程序处于后台或终止状态,这两个选项也能正常工作。

我希望这个解决方案可以解决您的问题。

解决这个问题的关键似乎是通知生成器。以下 link 描述了一个比我总结的更好的工作解决方案:

https://hackernoon.com/android-location-tracking-with-a-service-80940218f561