Android – Stop a Service from a different class Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Android – Stop a Service from a different class without wasting too much if your time.

The question is published on by Tutorial Guruji team.

In the application i’m making I want to start an intent in one activity

Intent toonService = new Intent(Login.this, ToonService.class);
toonService.putExtra("toonName", result.getName());

Will the following code close the intent i just opened? If not how can i get it to?

Intent toonService = new Intent(MainActivity.this,ToonService.class);

the second piece of code would be called at a time completly unrelated to the first piece of code.


Well, assuming you only want one instance of this service running at once you could hold a static variable in the service class and access it from anywhere. Example;

public class ToonService extends Service{

    public static ToonService toonService;

    public ToonService(){
        toonService = this;


The constructor for ToonService now stores the created instance in the static variable toonService. Now you can access that service from anywhere from the class. Example below;


You could also handle multiple instances by having the class store a static List of running instances, rather than just the single instance. It is worth noting, that when you tell a service to stop, you are only requesting that it is stopped. Ultimately the Android OS will determine when it is closed.

We are here to answer your question about Android – Stop a Service from a different class - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji