stop service in android

AndroidService

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

onDestroyed()

is wrong name for

onDestroy()  

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

Solution 2 - Android

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!

Solution 3 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRavi BhattView Question on Stackoverflow
Solution 1 - AndroidkrekerView Answer on Stackoverflow
Solution 2 - AndroiddondondonView Answer on Stackoverflow
Solution 3 - AndroidJorgesysView Answer on Stackoverflow