Android 即使应用程序被终止,也要保持跟踪前台服务的活动状态

Android 即使应用程序被终止,也要保持跟踪前台服务的活动状态,android,service,kill-process,foreground-service,Android,Service,Kill Process,Foreground Service,我正在开发一个应用程序,我需要通过GPS跟踪用户。我正在使用一项服务来控制位置,但当我关闭应用程序并将其关闭时,服务停止。我已尝试使用通知栏连接该服务,但问题仍然存在。我在几篇文章中读到过很多解决方案,但我不能像我需要的那样让它们继续存在。我的目的是让服务一直运行,即使应用程序被终止。我不知道这是否可能 在主活动中,我将启动服务: private void iniciarServicioGPS() { Intent service = new Intent(this, Loca

我正在开发一个应用程序,我需要通过GPS跟踪用户。我正在使用一项服务来控制位置,但当我关闭应用程序并将其关闭时,服务停止。我已尝试使用通知栏连接该服务,但问题仍然存在。我在几篇文章中读到过很多解决方案,但我不能像我需要的那样让它们继续存在。我的目的是让服务一直运行,即使应用程序被终止。我不知道这是否可能

在主活动中,我将启动服务:

 private void iniciarServicioGPS() {
        Intent service = new Intent(this, LocationService.class);
        startService(service);
}
LocationService.class:

 public class LocationService extends Service {
 Intent intent;
@Override
public void onCreate() {
    super.onCreate();
    Log.i("GPS", "onCreate");
    intent = new Intent(BROADCAST_ACTION);
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void start (Intent intent, int startId){
    Log.i("GPS", "onStart antes location manager");
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();        
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
}

 public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("gps", "Received Start Foreground Intent ");
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.amu_bubble_mask);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("ELE")
            .setTicker("ELE")
            .setContentText("Servicio de tracking activo")
            .setSmallIcon(R.drawable.amu_bubble_mask)
            .setLargeIcon(
                    Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .build();

    startForeground(Notification.FLAG_ONGOING_EVENT,notification);

    return START_REDELIVER_INTENT;
}
}

你说“我杀了它”是什么意思?是关闭所有活动、从最近列表中删除应用程序、从设置中执行“强制停止”还是其他操作?如果我关闭所有活动,服务将在没有通知的帮助下继续工作。我指的是通过从app recent list@SergioSo中删除该app来终止该app,在将app从recents中刷出后,您是否看到状态栏“Servicio de tracking activo”通知?不,我没有看到。当我刷卡时,状态栏不见了,真奇怪。因为一旦调用了
startForeground()
,关联的通知就不应该消失,即使在使用recents list进行操作之后。您是否在某处调用了
stopForeground()
?你也检查过替代设备上的这种行为吗?你说“我杀了它”是什么意思?是关闭所有活动、从最近列表中删除应用程序、从设置中执行“强制停止”还是其他操作?如果我关闭所有活动,服务将在没有通知的帮助下继续工作。我指的是通过从app recent list@SergioSo中删除该app来终止该app,在将app从recents中刷出后,您是否看到状态栏“Servicio de tracking activo”通知?不,我没有看到。当我刷卡时,状态栏不见了,真奇怪。因为一旦调用了
startForeground()
,关联的通知就不应该消失,即使在使用recents list进行操作之后。您是否在某处调用了
stopForeground()
?您是否在其他设备上检查过这种行为?