stop service in android

Ravi Bhatt picture Ravi Bhatt · Apr 5, 2011 · Viewed 183.7k times · Source

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.

Answer

kreker picture kreker · Jan 24, 2012
onDestroyed()

is wrong name for

onDestroy()  

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