【问题标题】:Android: Multiple IntentServices - stopServiceAndroid:多个 IntentServices - stopService
【发布时间】:2012-08-23 17:20:51
【问题描述】:

我多次使用唯一的 id 值启动 IntentService。当我重新创建意图并使用它在当前 IntentService 上调用 stopService 时,整个 IntentService 队列在停止当前 IntentService 之上被取消。此外,对队列中的 IntentService 调用 stopService 无效。

启动服务:

Intent intent = new Intent(context, IntentService.class);
intent.putExtra("action", "scan");
intent.putExtra("id", id);
context.startService(intent);

停止服务

Intent intent = new Intent(context, IntentService.class);
intent.putExtra("action", "scan");
intent.putExtra("id", id);
context.stopService(intent);

我希望停止/从队列中删除具有关联 ID 的 IntentService,然后让队列的其余部分运行。

【问题讨论】:

    标签: android intentservice


    【解决方案1】:

    我认为您可能必须在这里实现自己的意图服务类型。 Looking at the code

    虽然查看stopSelf(int) 方法,但如果您知道每个意图是用什么创建的,您也许可以手动执行此操作。

    mActivityManager.stopServiceToken(
                    new ComponentName(this, mClassName), mToken, startId);
    

    比如:

    mActivityManager.stopServiceToken(
                    new ComponentName(this, IntentService.class.getSimpleName()), null, mStartId);
    

    我还没有尝试过,但可能会朝着正确的方向轻推。虽然我认为它可能不会做任何事情,因为它只会停止运行服务。

    否则,您将不得不创建自己的 IntentService,您可以在其中触发删除意图。可能值得轻推@commonsguy

    干杯,
    克里斯

    【讨论】:

    • @meeeee 你在这方面有什么收获吗?我很想看看结果,因为我想我将来可能需要类似的东西。
    • 是的,我扩展了一个普通服务,每次调用 startService 时,我都会将意图添加到 arraylist 并启动工作线程(如果尚未启动)。工作线程循环遍历所有意图,我还使用一个意图调用 startService,使用一个标志从队列(arraylist)中删除另一个意图。
    • @meeeee 好的,我认为这可能是必须要做的。听起来不错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多