IntentService已销毁,但该服务仍在Android 2.3.4中记录数据
我有一个Android应用程序。 在一项活动中,我启动了如下服务:IntentService已销毁,但该服务仍在Android 2.3.4中记录数据,android,android-intent,android-service,sticky,intentservice,Android,Android Intent,Android Service,Sticky,Intentservice,我有一个Android应用程序。 在一项活动中,我启动了如下服务: Intent startIntent = new Intent(_context, HandlingService.class); _context.startService(startIntent); HandlingService的定义如下: public class HandlingService extends IntentService 那么在HandlingService内部,我有一个: @Override pu
Intent startIntent = new Intent(_context, HandlingService.class);
_context.startService(startIntent);
HandlingService的定义如下:
public class HandlingService extends IntentService
那么在HandlingService内部,我有一个:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
Log.v(ApplicationName,"HandlingService.onStartCommand");
if ((flags & START_FLAG_RETRY) == 0){
Log.v(ApplicationName,"Service is restarting");
}
return START_STICKY;
}
这是:
@Override
protected void onHandleIntent(Intent sourceIntent) {
Log.v(ApplicationName, "HandlingService.onHandleIntent");
sendSomething();
}
最后:
protected void sendSomething() {
while (numberOfTry > 0) {
numberOfTry++;
Log.v(ApplicationName,"HandlingService.sending Something. Try# " + numberOfTry);
}
}
numberOfTry从1开始
然后,在启动服务的活动中,当我单击“取消”按钮时,我称之为:
Intent stopIntent = new Intent(CallingActivity.this, HandlingService.class);
CallingActivity.this.stopService(stopIntent);
我看到有人调用HandlingService.OnDestroy,但我一直看到日志上写着“HandlingService.sending Something.Try#”和不断增加的数字
问:如果我已经通过呼叫stopService停止了它,为什么它还活着
提前谢谢!Guillermo。明确指出,您不应在派生类中重写onStartCommand()
。您可能弄乱了IntentService
的内部机制
直接从服务
派生类,或者使用IntentService
已经提供给您的框架(用于启动/停止自身)。明确指出,您不应在派生类中重写onStartCommand()
。您可能弄乱了IntentService
的内部机制
直接从
服务
派生类,或者使用IntentService
已经提供给您的框架(用于启动/停止自身)。来自IntentService javadoc
public void setIntentRedelivery(启用布尔值)
设置意图重新交付首选项。通常使用首选语义从构造函数调用
如果enabled为true,onStartCommand(Intent,int,int)将返回START\u REDELIVER\u Intent,因此如果此进程在onHandleIntent(Intent)返回之前终止,则进程将重新启动并重新交付Intent。如果发送了多个意向,则只保证重新交付最近的意向
如果enabled为false(默认值),onStartCommand(Intent,int,int)将返回START\u NOT\u STICKY,如果进程终止,Intent将随之终止。来自IntentService javadoc public void setIntentRedelivery(启用布尔值) 设置意图重新交付首选项。通常使用首选语义从构造函数调用 如果enabled为true,onStartCommand(Intent,int,int)将返回START\u REDELIVER\u Intent,因此如果此进程在onHandleIntent(Intent)返回之前终止,则进程将重新启动并重新交付Intent。如果发送了多个意向,则只保证重新交付最近的意向
如果enabled为false(默认值),onStartCommand(Intent,int,int)将返回START\u NOT\u STICKY,如果进程终止,Intent将随之终止。但是如果我不重写onStartCommand,如何使其变为STICKY?顺便说一句,为什么他们可以覆盖它,但在文档中他们说“不要这样做”?
IntentService
有自己的启动/停止语义。它启动一个工作线程,处理它在队列中的工作,并在完成时停止自身。您不需要覆盖任何内容。它只是按照文档记录的那样工作。如果您不喜欢它的工作方式,您只需创建自己的服务来扩展服务
。IntentService
已经提供的启动/停止语义有什么问题?您试图做的哪些操作不符合IntentService
的默认行为?该服务需要持续运行,直到达到某个条件,我不想只执行一次。然后您就不想使用IntentService
。只需实现您自己的服务,它扩展了服务
IntentService
的特定语义。如果您不想使用IntentService
的行为,则不应使用它。如果你想拥有后台线程,那么你需要启动它们并在你的服务中自己管理它们。是的,onStartCommand()
在主线程上运行。但是如果我不重写onStartCommand,我将如何使它保持粘性?顺便说一句,为什么他们可以覆盖它,但在文档中他们说“不要这样做”?IntentService
有自己的启动/停止语义。它启动一个工作线程,处理它在队列中的工作,并在完成时停止自身。您不需要覆盖任何内容。它只是按照文档记录的那样工作。如果您不喜欢它的工作方式,您只需创建自己的服务来扩展服务
。IntentService
已经提供的启动/停止语义有什么问题?您试图做的哪些操作不符合IntentService
的默认行为?该服务需要持续运行,直到达到某个条件,我不想只执行一次。然后您就不想使用IntentService
。只需实现您自己的服务,它扩展了服务
IntentService
的特定语义。如果您不想使用IntentService
的行为,则不应使用它。如果你想拥有后台线程,那么你需要启动它们并在你的服务中自己管理它们。是的,onStartCommand()
在主线程上运行。