Android:保持服务 运行

Android: Keep Service running

我想在后台保留一个 IntentService 运行,即使应用程序已被终止。但是,如果我从最近屏幕上删除我的应用程序,我的服务就会停止。我怎样才能避免这种情况?换句话说,即使我的应用已从最近的应用中关闭,我如何才能保持服务 运行?

我的前台服务:

public class ForegroundService extends Service {
private static final String LOG_TAG = "ForegroundService";
public static boolean IS_SERVICE_RUNNING = false;

private NotificationManagerCompat notificationManagerCompat;
final NotificationCompat.Builder playingChannelID = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID);


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notificationManagerCompat = NotificationManagerCompat.from(this);

    if (intent != null && intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
        showNotification();
    } else if (intent != null && intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        MainActivity.exoPlayer.setPlayWhenReady(false);
        notificationManagerCompat.cancel(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE);
        stopSelf();
    }
    return START_STICKY;
}
private void showNotification() {
    createNotificationChannel();

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Intent playIntent = new Intent(this, ForegroundService.class);
    playIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);
    Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.radio);

    playingChannelID.setContentTitle("Service");
    playingChannelID.setContentText("Playing...");
    playingChannelID.setSmallIcon(R.drawable.Service);
    playingChannelID.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false));
    playingChannelID.setContentIntent(pendingIntent);
    playingChannelID.setOngoing(true);
    playingChannelID.addAction(android.R.drawable.ic_delete, "Turn Off",
                    pplayIntent).build();notificationManagerCompat.notify(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, playingChannelID.build());
}
@Override
public void onDestroy() {
    super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
    // Used only in case if services are bound (Bound Services).
    return null;
}

private void createNotificationChannel() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = Constants.NOTIFICATION_NAME;
        String description = Constants.NOTIFICATION_DESCRIPTION;
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
}

我的常量:

public class Constants {
public static String NOTIFICATION_CHANNEL_ID = "NOTIFICATION_CHANNEL_ID_RADIO";
public static String NOTIFICATION_NAME = "RADIO_NOTIFICATION";
public static String NOTIFICATION_DESCRIPTION = "NOTIFICATION_DESCRIPTION";
public interface ACTION {
    public static String MAIN_ACTION = "com.marothiatechs.foregroundservice.action.main";
    public static String PLAY_ACTION = "com.marothiatechs.foregroundservice.action.play";
    public static String STARTFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.startforeground";
    public static String STOPFOREGROUND_ACTION = "com.marothiatechs.foregroundservice.action.stopforeground";
}
public interface NOTIFICATION_ID {
    public static int FOREGROUND_SERVICE = 101;
}
}

我的清单:

        <service android:name=".service.ForegroundService" >
        <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </service>

我看到应用程序打开时服务是 运行。当我通过主页按钮最小化应用程序时,它仍然是 运行。当我通过后退按钮关闭应用程序时,它仍然是 运行。但如果我如上所述杀死它,它就会停止。我该如何解决?

还不能发表评论,这就是我发布此 link 作为答案的原因。 我想你的问题可以从这个 link 得到回答: