Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to finish() method for Service class? To kill it dead

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?

like image 420
Kevik Avatar asked Dec 20 '12 09:12

Kevik


People also ask

How do you kill an activity from a service?

We can stop the services by stopSelf() and stopService(), in some cases android will kill the services due to the low memory problem.

Which method is used to kill the activity?

The onStop() and onDestroy() methods get called, and Android destroys the activity. A new activity is created in its place.

What finish () function does?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

Which method is called when Android system kills the activity due to memory issue?

A cached process is one that is not currently needed, so the system is free to kill it as desired when resources like memory are needed elsewhere.


2 Answers

use this line:

this.stopSelf();

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

like image 107
Lazy Ninja Avatar answered Oct 02 '22 01:10

Lazy Ninja


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

like image 23
ρяσѕρєя K Avatar answered Sep 30 '22 01:09

ρяσѕρєя K