stopSelf()vs stopSelf(int)vs stopService(Intent) [英] stopSelf() vs stopSelf(int) vs stopService(Intent)

查看:253
本文介绍了stopSelf()vs stopSelf(int)vs stopService(Intent)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

呼叫有什么区别
stopSelf()stopSelf(int)stopService(new Intent(this,MyServiceClass.class))
onStartCommand()吗?

What's the difference in calling
stopSelf() , stopSelf(int) or stopService(new Intent(this,MyServiceClass.class))
inside 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);
...

并以这种方式实现onStartCommand:

And implement onStartCommand in this way:

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;
}

三种方法的行为完全相同, 只有在两次执行onStartCommand之后,才会调用onDestroy.

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

推荐答案

我希望这会对您有所帮助:

I hope this will help you:

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

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().

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

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

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

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.

也就是说,当您调用stopSelf(int)时,您传递了停止请求所对应的开始请求的ID(传递给onStartCommand()的startId).然后,如果该服务在您能够调用stopSelf(int)之前收到了新的启动请求,则该ID将不匹配,该服务也将不会停止.

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()vs stopSelf(int)vs stopService(Intent)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆