有没有人知道是否有一种方法可以在不结束工作线程并停止自身的情况下停止IntentService?
这个问题很简单,但我在文档中找不到答案。有没有一种简单的方法来阻止它?
谢谢
发布于 2012-05-22 18:20:07
当发送到服务的消息进入队列时,调用onStartCommand。其转发用于排队的消息。所以你可以直接覆盖onStartCommand,就像这样:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("Stop"))
stopSelf();
onStart(intent, startId);
return START_NOT_STICKY;
}干杯
发布于 2011-09-14 04:39:26
您应该能够调用stopSelf();
http://developer.android.com/reference/android/app/Service.html#stopSelf()
发布于 2012-08-13 22:50:34
我最近偶然发现了我正在开发的一个应用程序的需求。我将尝试使用onStartCommand向Intent Service发送一条停止工作的消息(例如,设置一个布尔标志stopWork = true),并在工作作业期间或下一个排队任务之前对其进行评估。IntentService不会立即停止,但会跳过所有挂起的任务。希望能有所帮助。我自己也要试试。
https://stackoverflow.com/questions/7146624
复制相似问题