Search code examples
androidserviceandroid-serviceforeground-service

Android: Keep Service running


I want to keep a IntentService running in background even when the app is killed. But if I remove my app from the recents screen, my service is stopped. How can I avoid this? In other words, how can I keep my service running even if my app is closed from recent apps?

My ForegroundService:

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);
    }
}
}

My Constants:

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;
}
}

My Manifests:

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

I see that the service is running when the app is open. It's still running when I minimize the app via home-button. It's still running when I close the app via back-button. But it will stop if I kill it as mentioned above. How do I solve this?


Solution

  • Cant comment yet that's why Im posting this link as an answer. I think your question can be answered from this link : https://stackoverflow.com/a/42149611/6218490