Stopping an IntentService

元气小坏坏 提交于 2019-12-07 06:05:43

问题


Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?

Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?

Thanks


回答1:


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




回答2:


You should be able to call stopSelf();

http://developer.android.com/reference/android/app/Service.html#stopSelf()




回答3:


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.



来源:https://stackoverflow.com/questions/7146624/stopping-an-intentservice

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