stopSelf()与stopSelf(startId)的区别


startId:表示启动服务的次数

stopSelf(startId):表示停止startId次的服务,其他次数不停止。一般用于多次启动服务,多次执行onStartCommand(Intent intent, int flags, int startId)的情况;
stopSelf():表示直接停止服务(所有次数)

使用场景:
如果同时有多个服务启动请求发送到onStartCommand(),不应该在处理完一个请求后调用stopSelf();因为在调用此函数销毁service之前,可能service又接收到新的启动请求,如果此时service被销毁,新的请求将得不到处理。此情况应该调用stopSelf(int startId)

猜你喜欢

转载自luke-feng.iteye.com/blog/2291231