BroadcastReceive如何告诉Service?
在主要活动上,单击一个按钮启动服务,
该服务将执行这些工作:
1,今天随机 10 个时间戳
2,当每个时间戳到来时,它将执行一项任务,
现在我已经编写了函数 1,当第一个时间戳出现时来吧,我可以开始第一个任务,
我的方法是通过 Alarmmanager 注册一个广播接收器的时间戳,当触发第一个广播时,然后注册第二个时间戳,依此类推......
这是我的问题: 当广播接收器被触发时,如何告诉服务注册下一个时间戳,然后完成整个工作?
或者这是实施我的服务的其他方式?
提前致谢!
on the main activity click a button to start a service
the service will do those jobs:
1,random 10 timestamp for today
2, when each timestamp comes, it will do a task
now i have write the function 1, and when the first timestamp come, i could start the first task
my way is to register a broadcastreceiver for timestamp through alarmmanager, when first broadcast is triggered, then register the second timestamp and so on.....
Here is my question:
when the broadcastreceiver is triggered, how to tell the service to register the next timestamp, then finish the whole job?
Or is that other way to implement of my service?
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不应该这样做。
我可以看到你的方法至少有两个缺点:
- 警报管理器会太忙
- 您无法从另一个广播接收器注册广播接收器
另一种更好的方法是使用 handler.postDelayed 方法:
您的服务应该要求处理程序在几秒钟内返回(当您需要时)
因此,广播接收器没有办法。
我忘了说您应该在清单文件中为两个意图操作注册您的服务。
另外,最好添加单个操作,但要使用同名意图中的额外内容来区分 onStartCommand 应该执行的操作。然后,您的服务将在清单中注册单个操作。
下次,如果您觉得您的解释不太清楚,请发送一些代码/伪代码。
问候,
史蒂芬
You shouldn't do like this I think.
I can see at least two shortcomings in your method :
- the alarmanager will be too busy
- you can't register a brocast receiver from another broadcast receiver
Another, better way to do it would be to use a handler.postDelayed method :
Your service should ask a handler to come himself back in a few seconds (when you want)
Thus, there is no way for a broadcast receiver.
And I forgot to say that you should register your service for two intent actions in your manifest file.
Also, it could be nice to add a single action but to distinguish what onStartCommand should do using extras in intents with the same name. Then your service would register for a single action in your manifest.
Next time, if you feel your explanations are not so clear, please send some code / pseudo code.
Regards,
Stéphane