I created a sticky Foreground service which restarts every-time it gets killed, it seems to work fine but I noticed that in some cases ( when the OS kills it) it just wont restart. How can I make it restart EVERY time it gets killed? I want this service to run always.
Like those services.
This is my Foreground service onStartCommand:
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("InsiteMobile Service")
.setContentText("Run the app by clicking here")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();
Log.i("InsiteMobileLog", "Service Started");
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_STICKY;
}
And this is how i call the service in MainActivity onCreate():
public void startService() {
Intent serviceIntent = new Intent(this, AppService.class);
serviceIntent.putExtra("inputExtra", "InsiteMobileService");
ContextCompat.startForegroundService(this, serviceIntent);
}