Reputation: 1647
Is there a way to detect when my Android service restarts due to a crash? Could the service itself know about this after it was restarted? Or can the client applications get this information?
I tried to use the START_FLAG_REDELIVERY flag in the service but Im not sure how it works.
Upvotes: 3
Views: 10946
Reputation: 24073
From the onStartCommand(Intent intent, int flags, int startId)
documentation:
Parameters
intent - The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
So, if you service is restarted, for whatever reason (and it's started sticky), it will be restarted with a null intent.
Upvotes: 5
Reputation: 2913
Detect when the service restarts? This could be done by parsing the logcat, either manually or programmatically. Depending on what causes the "crash", there may be other ways..
Could the service know itself after it was restarted? Yes, through the redelivered intent received in the onCreate function.
Upvotes: 0