Android stopSelf() vs stopSelf(int) vs stopService(Intent)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22485298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:02:54  来源:igfitidea点击:

stopSelf() vs stopSelf(int) vs stopService(Intent)

androidservice

提问by GionJh

What's the difference in calling
stopSelf(), stopSelf(int)or stopService(new Intent(this,MyServiceClass.class))
inside onStartCommand()?

是什么在呼唤的差别
stopSelf()stopSelf(int)或者stopService(new Intent(this,MyServiceClass.class))
里面onStartCommand()

for example if I start the same services twice this way:

例如,如果我以这种方式启动相同的服务两次:

...
Intent myIntent1 = new Intent(AndroidAlarmService.this, MyAlarmService.class);
myIntent1.putExtra("test", 1); 
Intent myIntent2 = new Intent(AndroidAlarmService.this, MyAlarmService.class);
myIntent2.putExtra("test", 2);
startService(myIntent1);
startService(myIntent2);
...

And implement onStartCommand in this way:

并以这种方式实现 onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "onStartCommand called "+intent.getIntExtra("test", 0), Toast.LENGTH_LONG).show();
stopService(new Intent(this,MyAlarmService.class));
return START_NOT_STICKY;
}

I get exactly the same behaviour with the three methods, that is onDestroy will only be called after onStartCommand is executed twice.

这三种方法的行为完全相同,即 onDestroy 只会在 onStartCommand 执行两次后调用。

回答by SourabhTech

I hope this will help you:

我希望这能帮到您:

A started service must manage its own lifecycle. That is, the system does not stop or destroy the service unless it must recover system memory and the service continues to run after onStartCommand() returns. So, the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().

启动的服务必须管理自己的生命周期。也就是说,系统不会停止或销毁服务,除非它必须恢复系统内存并且服务在 onStartCommand() 返回后继续运行。因此,服务必须通过调用 stopSelf() 来停止自己,或者另一个组件可以通过调用 stopService() 来停止它。

Once requested to stop with stopSelf() or stopService(), the system destroys the service as soon as possible.

一旦使用 stopSelf() 或 stopService() 请求停止,系统会尽快销毁服务。

However, if your service handles multiple requests to onStartCommand() concurrently, then you shouldn't stop the service when you're done processing a start request, because you might have since received a new start request (stopping at the end of the first request would terminate the second one). To avoid this problem, you can use stopSelf(int) to ensure that your request to stop the service is always based on the most recent start request.

但是,如果您的服务同时处理对 onStartCommand() 的多个请求,则在处理完启动请求后不应停止该服务,因为您可能已经收到一个新的启动请求(在第一个请求结束时停止)请求将终止第二个)。为避免此问题,您可以使用 stopSelf(int) 来确保您停止服务的请求始终基于最近的启动请求。

That is, when you call stopSelf(int), you pass the ID of the start request (the startId delivered to onStartCommand()) to which your stop request corresponds. Then if the service received a new start request before you were able to call stopSelf(int), then the ID will not match and the service will not stop.

也就是说,当您调用 stopSelf(int) 时,您将传递停止请求所对应的启动请求的 ID(传递给 onStartCommand() 的 startId)。然后,如果服务在您能够调用 stopSelf(int) 之前收到一个新的启动请求,那么 ID 将不匹配并且服务不会停止。

回答by scottt

Here's a simplified description:

这是一个简单的描述:

  • stopSelf()is used to alwaysstop the current service.

  • stopSelf(int startId)is also used to stop the current service, but onlyif startIdwas the ID specified the last time the service was started.

  • stopService(Intent service)is used to stop services, but from outsidethe service to be stopped.

  • stopSelf()用于始终停止当前服务。

  • stopSelf(int startId)也可以用来停止当前的服务,但只有startId是ID指定的服务已启动最后一次。

  • stopService(Intent service)是用来停止服务的,但是从外面的服务被停止了。

This is the reference page.

这是参考页面。

回答by MKJParekh

The core functionality of all 3 methods are same and that's why they are having similar names. There are very small differences among them.

所有 3 种方法的核心功能都相同,这就是它们具有相似名称的原因。它们之间存在非常小的差异。

  • public final void stopSelf()
  • public final void stopSelf()

Class: This belongs to android.app.Serviceclass

班级:这属于android.app.Service班级

Called From: This method is intended to get called from insidethe service only.

调用自:此方法仅用于从服务内部调用。

Behavior: Service will get stopped. onDestroy()state called.

行为:服务将停止。onDestroy()状态调用。

  • public final void stopSelf(int startId)
  • public final void stopSelf(int startId)

Class: This belongs to android.app.Serviceclass

班级:这属于android.app.Service班级

Called From: This method is intended to get called from insidethe service only.

调用自:此方法仅用于从服务内部调用。

Behavior: There is a method from older version stopSelfResult(int startId). This method is newer version of that method. Service will get stopped onlyif the most recent time it was started was startId. onDestroy()state called.

行为:旧版本中有一个方法 stopSelfResult(int startId)。此方法是该方法的较新版本。当最近启动的时间是 startId 时,服务才会停止。onDestroy()状态调用。

May be you can find this method helpful only in a case where you started 2-3 instance of same service but you want to stop them in a order you received with startId list stored with you. Need to be very careful while using this method.

可能您会发现此方法仅在您启动 2-3 个相同服务实例的情况下有用,但您想按照收到的 startId 列表存储的顺序停止它们。使用此方法时需要非常小心。

  • public abstract boolean stopService(Intent service)
  • 公共抽象布尔停止服务(意图服务)

Class: This belongs to android.content.Contextclass

班级:这属于android.content.Context班级

Called From: This method is intended to get called from outsidethe service though you are allowed to call from inside.

调用自:此方法旨在从服务外部调用,尽管您可以从内部调用。

Behavior: If the service is not running, nothing happens. Otherwise it is stopped. Note that calls to startService() are not counted -- this stops the service no matter how many times it was started. onDestroy()state called.

行为:如果服务未运行,则什么也不会发生。否则停止。请注意,对 startService() 的调用不计算在内——无论服务启动多少次,这都会停止服务。onDestroy()状态调用。

回答by nawaab saab

stopService() is called from the class from where the Service is started. stopSelf() is called within the running service class to stop the Service

从服务启动的类调用 stopService()。在正在运行的服务类中调用 stopSelf() 以停止服务

回答by Neha - Systematix

stopSelf()=Stop the service, if it was previously started.

stopSelf(int startId)=Old version of stopSelfResult(int) that doesn't return a result.

stopSelfResult(int startId)=Stop the service if the most recent time it was started was startId.Its return boolean result.

stopSelf()=停止服务,如果它之前已经启动。

stopSelf(int startId)=旧版本的 stopSelfResult(int) 不返回结果。

stopSelfResult(int startId)=如果最近启动的时间是startId,则停止服务。它返回布尔结果。

回答by Sekhar Madhiyazhagan

stopSelf(int)- which is used to stop most recent start service based on onStartCommand(Intent i, int f, int startid)'s int startid.

stopSelf(int)- 用于根据 onStartCommand(Intent i, int f, int startid) 的 int startid 停止最近的启动服务。

stopSelf()- which is called by service itself , when task has been completed.

stopSelf()- 当任务完成时由服务本身调用。

stopService(Intent) - which is explicitly called from an activity to stop the service.

stopService(Intent) - 从活动中明确调用以停止服务。

回答by CommonsWare

There are two flavors of stopSelf(). One takes a startIdparameter, to indicate that you have completed work on one specific command, and so the service should opportunistically stop if there are no other outstanding commands.

有两种口味stopSelf()。一个接受一个startId参数,表示您已经完成了一个特定命令的工作,因此如果没有其他未完成的命令,服务应该机会停止。