[英]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. 它用广播接收器侦听USER_PRESENT
以显示一条消息。 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. 但是问题是,当Android内存不足并杀死它,然后重新启动它时,即使用户不只是解锁设备,它也会再次显示该消息。 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? 我使用service.START_NOT_STICKY
,但是service.START_STICKY
可以胜任这项工作吗? I guess I don't fully understand the differences but I'm pretty sure I want NOT_STICKY
. 我猜我不太了解这些差异,但是我很确定我想要NOT_STICKY
。
You can set the action
field in the intent
to a specific string when you call your receiver and then check it in onReceieve
. 您可以在调用接收方时将intent
的action
字段设置为特定字符串,然后在onReceieve
中onReceieve
进行检查。
If it has your string then you called it , otherwise someone else. 如果它包含您的字符串,则您将其称为,否则调用其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.