BroadcastReceive如何告诉Service?

发布于 2024-11-10 02:06:16 字数 311 浏览 4 评论 0原文

在主要活动上,单击一个按钮启动服务,

该服务将执行这些工作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2024-11-17 02:06:16

我认为你不应该这样做。

我可以看到你的方法至少有两个缺点:
- 警报管理器会太忙
- 您无法从另一个广播接收器注册广播接收器

另一种更好的方法是使用 handler.postDelayed 方法:

您的服务应该要求处理程序在几秒钟内返回(当您需要时)

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {    
    //do some treatment based on the action intent
    //first action is used to compute 10 time stamps

    //second action is used to treat task

    //then use a handler to reschedule your service
    handler.postDelayed( new Runnable() {
        @Override
        public void run() {
           onStartCommand( <intent for action 2>, flags, startId);
           Log.v( LOG_TAG, "timer sending intent");
        }
    }, delayInMsToNextTask  );
}//met

因此,广播接收器没有办法。

我忘了说您应该在清单文件中为两个意图操作注册您的服务。

<service android:name="myService">
    <intent-filter>
      <action android:name="action1" />
    </intent-filter>
    <intent-filter>
      <action android:name="action2" />
    </intent-filter>
</service>

另外,最好添加单个操作,但要使用同名意图中的额外内容来区分 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)

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {    
    //do some treatment based on the action intent
    //first action is used to compute 10 time stamps

    //second action is used to treat task

    //then use a handler to reschedule your service
    handler.postDelayed( new Runnable() {
        @Override
        public void run() {
           onStartCommand( <intent for action 2>, flags, startId);
           Log.v( LOG_TAG, "timer sending intent");
        }
    }, delayInMsToNextTask  );
}//met

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.

<service android:name="myService">
    <intent-filter>
      <action android:name="action1" />
    </intent-filter>
    <intent-filter>
      <action android:name="action2" />
    </intent-filter>
</service>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文