代码之家  ›  专栏  ›  技术社区  ›  ketan dhopeshwarkar

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

  •  18
  • ketan dhopeshwarkar  · 技术社区  · 7 年前

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

    startService(new Intent(this, GpsServices.class));
    

    然后,当应用程序处于空闲模式(应用程序处于后台或kill)时,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();
            }
        }
    }
    

    任何帮助都将不胜感激。

    2 回复  |  直到 7 年前
        1
  •  22
  •   Amrut Bidri    6 年前

    你可以尝试以下两种选择中的一种或两者的组合,当我面对问题时,它们解决了我的问题。

    选项1

    要在后台继续运行位置更新,必须使用 LocationServices API 具有 FusedLocationProviderClient 如上所述 here here 在文档中或 here

    选项2

    如果您已经正确阅读了Android Oreo 8.0文档 here ,则您将获得此解决方案。

    步骤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 它将继续显示 通知 直到您的 GpsService 正在上运行的所有设备上运行 Android Oreo 8.0 .

    我确信,即使应用程序处于后台或kill状态,这两个选项也会起作用。

    我希望这个解决方案能解决你的问题。

        2
  •  1
  •   Luke Dupin    6 年前

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

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