【问题标题】:In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill在 Oreo (8.0.0+) (API 26+) 中,如何在应用程序处于后台或终止时获取位置服务更新
【发布时间】:2018-06-24 05:53:51
【问题描述】:

在 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();
        }
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 你应该阅读如何在后台处理位置developer.android.com/about/versions/oreo/…
  • 如果您能提供解决方案,那将很有帮助。我已经阅读了您提供的链接。但是不推荐使用 GeofencingApi 和 FusedLocationProviderApi 接口。
  • 请阅读链接,使用这些与实际问题无关

标签: android background geolocation android-service android-8.0-oreo


【解决方案1】:

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

选项 1

要使位置更新继续在后台运行,您必须使用LocationServices APIFusedLocationProviderClient,如文档中的herehere 或CODEPATH 中的here 所述。

选项 2

如果您在 here 的某个地方正确阅读了 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));
        }

第 2 步:使用通知来表明您的服务正在运行。在onCreate服务方法中添加以下代码行。

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

第 3 步:在服务停止或销毁时删除notification

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

选项 2 的一个问题是它会一直显示notification,直到您的GpsService 在所有运行Android Oreo 8.0 的设备上运行。。 p>

我确信即使应用程序处于后台或终止状态,这两个选项都可以工作。

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

【讨论】:

  • 我们可以在启动前台服务之前检测到 GPS 开/关吗?
  • 这个 (GPS) 解决方案在 8.0 中不再适用于我
  • @moDev 你有没有找到任何解决方案来检测 android (Oreo)8.0 中的 GPS ON/OFF。因为广播接收器不再用于检测 GPS 开/关。如果您找到任何解决方案,请发布答案。
  • @Suresh 在 8.0 中无法检测 GPS 开/关
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多