alternative to finish() method for Service class?

2019-04-19 08:34发布

I have used the method finish() before in several activities without problems.

however when I tried to call it inside of the broadcast receiver I get the error message from the compiler that "the method finish() is undefined for the type AudioService"

AudioService is the name of my Service class in my Android app.

If there is no finish() method in a Service than what can I call to kill this Service dead?

2条回答
该账号已被封号
2楼-- · 2019-04-19 09:08

you can try as to stop your AudioService service from broadcast receiver :

Intent intent = new Intent();
intent.setClass(context, AudioService.class); 
context.stopService(intent);

where context is first param which you received in on Receive of broadcast receiver

查看更多
forever°为你锁心
3楼-- · 2019-04-19 09:09

use this line:

this.stopSelf();

to kill itself.
Or from outside use stopService(intent);

查看更多
登录 后发表回答