【问题标题】:How to keep a Service running when app is minimized最小化应用程序时如何保持服务运行
【发布时间】:2018-01-10 12:53:16
【问题描述】:

我正在开发一个跟踪用户位置的应用程序。 我已经实现了一个管理位置请求部分的自定义服务,它在每个活动中都能正常工作。该服务甚至出现在“运行服务”设置面板中。

当我最小化应用程序或锁定屏幕时,问题就开始了。

我想要的效果是即使在应用程序被最小化或屏幕被锁定时也能继续接收位置更新,但是当用户从最近的应用程序中滑动应用程序时停止所有与应用程序相关的内容(谷歌地图或 Waze 的行为方式 - 显示最小化应用程序时的通知 - 可以直接从通知中关闭应用程序)。

我已经尝试了很多建议的解决方案,唯一接近的一个是 startForegroundService(),但即使应用程序被关闭也不会停止服务。

我正在 Google Pixel (8.1) 和具有 5.0 和 8.0 的模拟器上运行测试。

最低 SDK 版本:21。目标 SDK 版本:26。

这是我目前的代码:

AndroidManifest.xml

<service android:name=".logic.service.LocationService"/>

MainActivity.java

if (checkNeedLocationPermission(this)) {
        startService(new Intent(getBaseContext(), LocationService.class));
    }

LocationService.java

public class LocationService extends Service {

private FusedLocationProviderClient fusedLocationProviderClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;

@Override
public IBinder onBind(final Intent intent) {
    return null;
}

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    startFusedLocationProviderClient();
    return START_STICKY;
}

@Override
public void onDestroy() {
}

/**
 * Configures and starts Google Api Client for location services.
 */
private void startFusedLocationProviderClient() {
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    getCurrentLocation();
}

/**
 * Gets the current location of the device.
 */
@SuppressLint("MissingPermission")
private void getCurrentLocation() {
    createLocationRequestAndCallback();

    fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
}

/**
 * Creates the location request with the specified settings.
 */
private void createLocationRequestAndCallback() {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(LOCATION_REQUEST_INTERVAL);
    locationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST_INTERVAL);
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(final LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                Log.d("lat", String.valueOf(location.getLatitude()));
            }
        }
    };
}

在上面的类中,LocationService.java, Log.d("lat", String.valueOf(location.getLatitude())); 在应用最小化或屏幕锁定时停止记录。

任何帮助或建议将不胜感激!

【问题讨论】:

  • 您有什么解决方案还是我应该粘贴我的代码?我有同样的情况,我一切都很好。请告诉我
  • @nihal_softy 他在 7 分钟前提出了这个问题;他还没有找到解决办法。是的,你应该写一个答案,但 Stack Overflow 不仅仅是一个代码共享服务。不要简单地转储您的代码 - 解释解决方案,显示相关的 sn-ps 以获取详细信息,最好包括参考文档的链接。
  • @JonathonReinhart 我欢迎您的建议先生。我将在这里尝试解释。实际上我也试过这个,但在我的情况下它工作正常......我不知道他为什么会遇到问题
  • 我目前没有任何可行的解决方案,但我正在尝试更多建议。另外, android:stopWithTask="false" 对我不起作用。

标签: java android service


【解决方案1】:

从 Android Oreo 开始,您不能这样做,因为它们引入了后台服务限制。参考documentation

但是对于低于 26 的 API 级别,您可以通过设置来实现这一点

 <service
            android:name=".YourLocationService"
            android:exported="false"
            android:process=":YourLocationService"
            android:stopWithTask="false" />

@Override
    public void onTaskRemoved(Intent rootIntent) {

        Intent restartService = new Intent(getApplicationContext(), this.getClass());
        PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, 5000, pendingIntent);


    }

【讨论】:

    猜你喜欢
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多