-1

I want to restart the service after the activity is destroyed.

In the service is a database-reference to a firebase-db. The purpose of this service is to show a notification to the user, if a child was added.

My Manifest.xml looks as follow:

<application>
[...]
<service
    android:name=".FirebaseComponent.FirebaseDatabasePushService"
    android:process=":PushService"
    android:enabled="true"
    android:exported="false" />
</application>

I tried to use public void onTaskRemoved(Intent rootIntent) and inside a startService(intent) but that didn't worked either.

My onStartCommand(Intent intent, int flags, int startId) returns START_STICKY.

How do I achieve, that the service is restarted after the user killed it?

Charlie
  • 1,169
  • 2
  • 16
  • 31

1 Answers1

1

To my knowledge there is no way to restart a service at this point in time. I remember that I had this question a few months ago, and the only solution was to

  1. Stop the service completely and
  2. Run a postDelayed to restart it after, say, 5 seconds.

Hope this helps!

Ethan
  • 1,905
  • 2
  • 21
  • 50
  • is there a good point in time to restart the service? Or maybe can the OS starts the service? – Charlie Jul 24 '16 at 17:56
  • Yes, you can set the service to run on device boot if that works better for you. Otherwise you want to put a time delay so the OS can process the service shutting down before restarting it. – Ethan Jul 24 '16 at 17:59
  • what do you mean with OS can restarting it? how? – Charlie Jul 24 '16 at 18:10
  • In the manifest you can adda permission to start the service when the device restarts or even turns on period. But you will have to stop and then restart if you don't want the entire phone to have to shut down – Ethan Jul 24 '16 at 18:17
  • Currently the App is open the service running. I stop the app and switch to an other the service is stopped. I do not want, that the service is stopped. Any sugestions how to solve this problem? – Charlie Jul 24 '16 at 18:24
  • You can start that service with `START_STICKY`, that'll run the service continuously. But it wouldn't be too efficient in terms of battery usage – Ethan Jul 24 '16 at 18:49
  • How to do that? I think, that works only, if the OS and only the os stops the service. But if the user leaves the app, it won't be recreated. – Charlie Jul 24 '16 at 18:52
  • The service will never stop if the user exits the app, so it won't have to be restarted. Please accept my answer as I've answered your main question adequately. – Ethan Jul 24 '16 at 19:03
  • But it is stopped. I don't see the service anymore – Charlie Jul 24 '16 at 19:12
  • Try this, it should help you more http://stackoverflow.com/questions/21450287/android-run-service-continuously – Ethan Jul 24 '16 at 19:19
  • this can't be the solution, because this will awake the device every x seconds. But this would be the workaround. – Charlie Jul 24 '16 at 19:35
  • Then when you call `onDestroy` place a `postDelayed` in there and see if that works. I don't know what else I can say because you're not being very clear at all – Ethan Jul 24 '16 at 19:40
  • Think about it like a chat app. If the user is in the app, he do not want any notification on top (handled in the app). If he is isn't in the app, he should get one. – Charlie Jul 24 '16 at 19:41
  • That can be handled using a simple `boolean`, If `onCreate` is called then `isActive` is true. If `onDestroy` is called `isActive!` and that is where you start the service from. Save the value of `isActive` to `sharedPreferences` and call it a day – Ethan Jul 24 '16 at 22:39