88

Here I tried simple service program. Start service works fine and generates Toast but stop service does not. The code of this simple service is as below:

public class MailService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    public void onCreate(){
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
    }
    public void onDestroyed(){
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}

The code of the Activity from where this Service is called is as below:

public class ServiceTest extends Activity{
    private Button start,stop;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.service_test);

        start=(Button)findViewById(R.id.btnStart);
        stop=(Button)findViewById(R.id.btnStop);

        start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startService(new Intent(ServiceTest.this,MailService.class));
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopService(new Intent(ServiceTest.this,MailService.class));
            }
        });
    }
}

Help me to stop service with that stop button which generates toast in the onDestroy() method. I have already seen many posts regarding stop service problem here, but not satisfactory so posting new question. Hope for satisfactory answer.

Romain Guidoux
  • 2,943
  • 4
  • 28
  • 48
Ravi Bhatt
  • 1,930
  • 1
  • 14
  • 27
  • 6
    Does `stopService(serviceIntent)` not work? – Chris Cashwell Apr 05 '11 at 17:43
  • *"Also, bear in mind that the exact timing of the service being destroyed is up to Android and may not be immediate."* From: http://stackoverflow.com/questions/2176375/android-service-wont-stop/2176415#2176415 – bigstones Apr 05 '11 at 17:47
  • 1
    @chris : I think stopService(serviceIntent) method as i implemented above not worked because Toast at onDestroy() not occured on stop button click. – Ravi Bhatt Apr 06 '11 at 07:09
  • 1
    @bigstone : does it mean that my stopService() and onDestroy() method works correctly but not immediately when i press stop button? – Ravi Bhatt Apr 06 '11 at 07:11
  • @Ravi: yes. Android might decide that there are enough resources to keep the service in memory, so that it will be ready if it's needed again. – bigstones Apr 06 '11 at 12:01
  • @bigstone: okay now i got somewhat idea about service. Thanks. – Ravi Bhatt Apr 06 '11 at 18:22

3 Answers3

53
onDestroyed()

is wrong name for

onDestroy()  

Did you make a mistake only in this question or in your code too?

k4dima
  • 6,070
  • 5
  • 41
  • 39
  • Yeah later i did my code from scratch and it worked well. Any yeah may be that problem in my earlier code which other and i also not noticed. Any way thanks :) As it is also help to other too. – Ravi Bhatt Jan 26 '12 at 09:03
  • 2
    @RaviBhatt So have you succeeded in stopping a service? If yes, Then can you share how to do it? – suraj Mar 31 '12 at 07:47
  • @suraj [what about docs?](http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle) – k4dima Apr 03 '12 at 23:25
  • 4
    @suraj If you have refer my code than what you need to do is, make one intent in Activity like `intent = new Intent(ServiceTest.this,MailService.class)` and use that same intent for starting and stopping the service like startService(intent) and stopService(intent). – Ravi Bhatt May 14 '12 at 17:31
  • 13
    That's why you should always use `@Override` annotation. – Pavel Apr 19 '14 at 16:38
24

This code works for me: check this link
This is my code when i stop and start service in activity

case R.id.buttonStart:
  Log.d(TAG, "onClick: starting srvice");
  startService(new Intent(this, MyService.class));
  break;
case R.id.buttonStop:
  Log.d(TAG, "onClick: stopping srvice");
  stopService(new Intent(this, MyService.class));
  break;
}
}
 }

And in service class:

  @Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.braincandy);
    player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

HAPPY CODING!

dondondon
  • 881
  • 8
  • 4
18

To stop the service we must use the method stopService():

  Intent myService = new Intent(MainActivity.this, BackgroundSoundService.class);
  //startService(myService);
  stopService(myService);

then the method onDestroy() in the service is called:

  @Override
    public void onDestroy() {

        Log.i(TAG, "onCreate() , service stopped...");
    }

Here is a complete example including how to stop the service.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268