Stopping an IntentService

▼魔方 西西 提交于 2019-12-05 11:04:37

bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals("Stop"))
        stopSelf();
    onStart(intent, startId);
    return START_NOT_STICKY;
}

cheers

I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!