Search code examples
javaandroidandroid-intentandroid-serviceandroid-broadcast

How do I know when my service is called from a broadcast receiver vs Android?


So, I have a service for my app that is always running if the user turns it on. It listens with a broadcast receiver for USER_PRESENT to show a message. It is only supposed to show a message on unlocking.

The problem is though, when Android runs out of memory and kills it, then restarts it, it will show the message again, even if the user hasn't just unlocked their device. Is there a way to know who called the service?

I use service.START_NOT_STICKY, but would service.START_STICKY be better for this job? I guess I don't fully understand the differences but I'm pretty sure I want NOT_STICKY.


Solution

  • You can set the action field in the intent to a specific string when you call your receiver and then check it in onReceieve.

    If it has your string then you called it , otherwise someone else.