Android 应用程序销毁时,某些手机中的前台服务停止

Android 应用程序销毁时,某些手机中的前台服务停止,android,foreground-service,Android,Foreground Service,当应用程序被破坏时,前台服务在某些手机中停止,即使应用程序被破坏,也希望运行前台服务 显示: 服务: } } 对于具有操作系统Oreo及以上版本的设备,运行服务有限制,您必须显示具有操作系统Oreo及以上版本的设备的通知,您必须再做一件事,而不是startService尝试使用StartForegroundsService获取更多信息,请查阅官方文档[你能把你到目前为止试过的代码包括进去吗?@Anil Kollaw你把代码包括进去了吗@AnilKolla@g.brahmaDatta在帖子中添加了

当应用程序被破坏时,前台服务在某些手机中停止,即使应用程序被破坏,也希望运行前台服务

显示: 服务: } }


对于具有操作系统Oreo及以上版本的设备,运行服务有限制,您必须显示具有操作系统Oreo及以上版本的设备的通知,您必须再做一件事,而不是startService尝试使用StartForegroundsService获取更多信息,请查阅官方文档[你能把你到目前为止试过的代码包括进去吗?@Anil Kollaw你把代码包括进去了吗@AnilKolla@g.brahmaDatta在帖子中添加了上面的代码,提前谢谢。试试这个@AnilKolla,我做的和你说的一样,仍然有问题。但是看起来你做的和上面的代码片段不一样。请现在查看我的帖子在启动服务之前,检查android版本是否为oreo及以上版本,然后调用startForgroundService,否则调用StartService再次查看我的代码Sujeet,但我仍然面临同样的问题。
<service android:name=".services.LocationService" />
private void startLocationUpdates() {
    if (!isMyServiceRunning(LocationService.class)) {

        Intent locationServiceIntent = new Intent(this, LocationService.class);
        locationServiceIntent.putExtra(StringConstants.LOCATION_UPDATES, true);

        startService(locationServiceIntent);
    }
}

private void stopLocationUpdates() {
    Intent locationServiceIntent = new Intent(this, LocationService.class);
    locationServiceIntent.putExtra(StringConstants.LOCATION_UPDATES, false);

    startService(locationServiceIntent);
}
public class LocationService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service started");
    if (intent != null) {
      if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForeground(1, notification);

      } else {

        startService(new Intent(getApplicationContext(), MyForeGroundService.class));

    }
    }
    return START_STICKY;
private NotificationCompat.Builder getNotificationBuilder(){
    return new NotificationCompat.Builder(this)
            .setContentIntent(MyApp.pendingIntent)
            .setContentText("setContentText")
            .setContentTitle(getString(R.string.app_name))
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_black_24px))
            .setSmallIcon(R.drawable.ic_arrow_back_black_24px);
}