Search code examples
androidservicepersistentforeground

How to kill a foregound service


I am developping an app that requires the use of a foreground service. The service does its job properly but i can't kill it. First, I tried to kill by using stopService, then i saw a topic where people said to use startService with an extra as a key to kill the service :

In my activity:

Intent i = new Intent(this, MyService.class);
i.putBooleanExtra("killService",true);

In my service's onStartCommand:

if(intent.getBooleanExtra("killService",false))
{
    stopForeground(true);
    stopSelf();
 ....

I want to stop the service when the user presses a specific button of my activity :

public void onClick(View v)
{
    switch(v.getId)
    {
       case R.id.myButton:
       (here i want to kill the service)
       break;
       ...

Solution

  • My mistake, onDestroy was being called but i wasnt able to see it...