简体   繁体   English

我怎么知道何时从广播接收器与Android调用我的服务?

[英]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 . 您可以在调用接收方时将intentaction字段设置为特定字符串,然后在onReceieveonReceieve进行检查。

If it has your string then you called it , otherwise someone else. 如果它包含您的字符串,则您将其称为,否则调用其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用广播接收器停止服务 - How can I stop my service using broadcast receiver 如何从广播接收机访问变量? - How do I access variables from a broadcast receiver? 如何在从广播接收器调用的Activity中创建TextToSpeech对象? - How can I create a TextToSpeech object in an Activity called from the broadcast receiver? 如何验证来自不同应用程序的广播接收器的 onReceive 方法是否被实际调用? - How can I verify that the onReceive method of a Broadcast Receiver from a different App is actually called? 如何在 Android API 30 设备中使用 Static 广播接收器或类似服务? - How can I use Static Broadcast Receiver or similar service in Android API 30 device? 如何将参数从调用服务的活动传递到广播接收器 - How to pass argument to broadcast receiver from an activity that call a service android广播接收器和服务获取通知 - android broadcast receiver and service to get notification 如何在不重新启动的情况下将广播接收器的信息发送回我的主要活动 - how can i send information from broadcast receiver back to my main activity without restarting it 如何找出广播接收机被称为? - how to find out broadcast receiver is called? 如何使用广播接收器向布局中的对象发送命令? - How Do I Use A Broadcast Receiver To Send Commands To Objects In A Layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM