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

ket*_*kar 15 android background geolocation android-service android-8.0-oreo

在Android O(8.0.0+)(API 26+)中,如何在应用程序处于后台或终止时获取位置服务更新.在"Strava"Android应用程序(在Play商店中可用),当应用程序在后台或终止时,位置服务可以正常运行.我需要在我的应用程序中构建相同的功能.每当我开始使用服务

startService(new Intent(this, GpsServices.class));
Run Code Online (Sandbox Code Playgroud)

然后当应用程序处于空闲模式(应用程序在后台或终止)时,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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Amr*_*dri 22

您可以尝试以下两个选项之一或两者的组合,这些选项在我遇到问题时解决了我的问题.

选项1

要使位置更新继续在后台运行,您必须使用LocationServicesAPI,FusedLocationProviderClient如此此处所述,或者在此处使用CODEPATH.

选项2

如果您已经在这里的某个地方正确阅读了Android Oreo 8.0文档,那么您将获得此解决方案.

步骤1:确保您将服务作为前台服务启动,如下面的代码所示

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));
        }
Run Code Online (Sandbox Code Playgroud)

第2步:使用通知显示您的服务正在运行.在onCreate服务方法中添加以下代码行.

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

步骤3:删除notification服务停止或销毁的时间.

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

选项2的一个问题是它将继续显示notification直到您GpsServiceAndroid Oreo 8.0上运行的所有设备上运行.

我确信即使应用程序处于后台或处于kill状态,这两个选项都可以正常工作.

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