I'm kind of new in Android and I realized that all the sounds are going out through the speaker regardless if the headphones are connected. If they are the sound also goes out through the headphones, but I'd also like to avoid the speaker when headphones connected.

Is that possible?

Few more information that may help:

My phone is a Samsung Galaxy i5510

Its Android 2.2 Froyo

I've installed Tasker, maybe there is some way to make a profile to achieve what I want.

Thanks, Diego

Update

As it wasn't to clear I'll clarify for each sound (if i forget any please let me know):

Phone calls: Speaker and headphones.

Notifications: Speaker and headphones.

Games and almost every app: Only headphones. (I wrote "almost every app" because, for example, Skype or Camera sounds in both speaker and headphones).

I saw you had another question about Cyanogenmod, and thought I'd point out that it has the option "Always play on speaker" for notifications, ringtones and alarms (in Settings -> CyanogenMod Settings -> Sound). So as soon as it's stable (if it's developed further) it can solve most of your problems.

@onik, thanks for the answer. I'll keep this in mind if sometime I install CyanogenMod, but right now there is no official release for my phone and I'm a little afraid of the issues of the unofficial version.
–
DiegoOct 12 '11 at 16:17

I am afraid, but this doesn't seem possible. There once was an method called setRouting() in the AudioManager API, but it is nowadays a deprecated method which simply does nothing. I am even not sure if the method would be capable of routing all audio streams only to the (wired) headset.